www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Library and site D examples

reply Karmello <Karmello.Kyzer BasicMail.host> writes:
should automatically include most of the standard library so one 
doesn't have to constantly import them just to do basic playing 
around with the examples.


For example, I modified an example

import std.array : assocArray;
import std.range : enumerate;
import std.conv, std.algorithm, std.math;

auto aa = iota(1,20).map!(i => round(log(i))).take(10).array;
writeln(to!string(aa));


and had to import the 3rd line. This gets pretty annoying when 
playing around with the examples. The most common libs should 
automatically be imported and may be dependent on the 
library(e.g, if its std.traits maybe import std.meta and others 
related).

Also, this example works but in visual stupid the same code gives 
me an error:

Error: function `X.Music.map!(Result).map` need `this` to access 
member `map`
X.d(22):        called from here: `map(iota(1, 100))`
X.d(22):        called from here: `take(map(iota(1, 100)), 30u)`

The difference is that I'm trying to generate this sequence at 
compile time(it's initializing a struct member). It should be 
computable without issue and I'm just replacing hard coded array. 
Seems iota isn't being computed at compile time.

struct x
{
     static:
       auto a = iota(1,1000).map!(i => 
round(log(i))).take(30).array;
}
Jan 28 2023
next sibling parent reply Tejas <notrealemail gmail.com> writes:
On Sunday, 29 January 2023 at 00:03:31 UTC, Karmello wrote:
 should automatically include most of the standard library so 
 one doesn't have to constantly import them just to do basic 
 playing around with the examples.


 For example, I modified an example

 import std.array : assocArray;
 import std.range : enumerate;
 import std.conv, std.algorithm, std.math;

 auto aa = iota(1,20).map!(i => round(log(i))).take(10).array;
 writeln(to!string(aa));


 and had to import the 3rd line. This gets pretty annoying when 
 playing around with the examples. The most common libs should 
 automatically be imported and may be dependent on the 
 library(e.g, if its std.traits maybe import std.meta and others 
 related).
You can just `import std;` if you want to play around and don't care much about the first compilation time. The following code also works: ```d import std; void main(){ auto aa = iota(1,20).map!(i => round(log(i))).take(10).array; writeln(to!string(aa)); } ```
Jan 28 2023
parent Karmello <Karmello.Kyzer BasicMail.host> writes:
On Sunday, 29 January 2023 at 03:49:04 UTC, Tejas wrote:
 On Sunday, 29 January 2023 at 00:03:31 UTC, Karmello wrote:
 should automatically include most of the standard library so 
 one doesn't have to constantly import them just to do basic 
 playing around with the examples.


 For example, I modified an example

 import std.array : assocArray;
 import std.range : enumerate;
 import std.conv, std.algorithm, std.math;

 auto aa = iota(1,20).map!(i => round(log(i))).take(10).array;
 writeln(to!string(aa));


 and had to import the 3rd line. This gets pretty annoying when 
 playing around with the examples. The most common libs should 
 automatically be imported and may be dependent on the 
 library(e.g, if its std.traits maybe import std.meta and 
 others related).
You can just `import std;` if you want to play around and don't care much about the first compilation time. The following code also works: ```d import std; void main(){ auto aa = iota(1,20).map!(i => round(log(i))).take(10).array; writeln(to!string(aa)); } ```
Yes, that code works, that code is not what I'm doing. They can add import std; to every line of all the examples.
Jan 29 2023
prev sibling next sibling parent reply Karmello <Karmello.Kyzer BasicMail.host> writes:
 struct x
 {
     static:
       auto a = iota(1,1000).map!(i => 
 round(log(i))).take(30).array;
 }
The solution here to put the array code in to a function that returns it. It's excessive. Why map needs to be hidden inside a function to get it to compile is beyond me.
Feb 01 2023
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 2/1/23 13:17, Karmello wrote:
 struct x
 {
     static:
       auto a = iota(1,1000).map!(i => round(log(i))).take(30).array;
 }
The solution here to put the array code in to a function that returns it. It's excessive. Why map needs to be hidden inside a function to get it to compile is beyond me.
It doesn't have to be inside a function. For example, it is used when computing the initial value of a module variable here: import std.range : array, iota, take; import std.algorithm : map; import std.math : log, round; auto a = iota(1,1000).map!(i => round(log(i))).take(30).array; void main() {} Statements cannot go to the module scope. Perhaps that's the limitation you see. Regarding applying "import std;" universally, it does not work for the code above because the compiler cannot choose between std.logger's log and std.math's log. Ali
Feb 01 2023
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 1/28/23 7:03 PM, Karmello wrote:
 struct x
 {
      static:
        auto a = iota(1,1000).map!(i => round(log(i))).take(30).array;
 }
This is a bug in the compiler. Change your function to not be a templated lambda, and it works: ```d struct x { static: auto a = iota(1,1000).map!((int i) => round(log(i))).take(30).array; } ``` It can be reduced to: ```d struct x { static: auto a = [1,2,3].map!(i => i); // fails auto b = [1,2,3].map!((int i) => i); // works } ``` From testing on run.dlang.io, this hasn't worked all the way back to 2.060. There is already a bug report on this: https://issues.dlang.org/show_bug.cgi?id=20077 -Steve
Feb 02 2023