| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
January 22, 2015 generate an array of 100 uniform distributed numbers | ||||
|---|---|---|---|---|
| ||||
hi guys, firstly this has no direct application, i'm just playing around and learning i want to create 100 uniform distributed numbers and print them my first attempt, just written by intuition: [0 .. 100].map!(v => uniform(0.0, 1.0).writeln); i found out i can't write [0 .. 100] to define a simple number range, but is there a function to do so? second attempt, replacing the range with an simple array [0,1,2].map!(v => uniform(0.0,1.0).writeln); this does compile and run, but doesn't print anything, just an empty string, why is that? finally i got it working with this: auto t = [0,1,2].map!(v => uniform(0.0,1.0)); writeln(t); seems pretty easy eh? d is bugging me alot like this ^_^ but i love it's syntax | ||||
January 22, 2015 Re: generate an array of 100 uniform distributed numbers | ||||
|---|---|---|---|---|
| ||||
Posted in reply to ddos | On Thursday, 22 January 2015 at 19:26:46 UTC, ddos wrote: > hi guys, firstly this has no direct application, i'm just playing around and learning > > i want to create 100 uniform distributed numbers and print them > my first attempt, just written by intuition: > [0 .. 100].map!(v => uniform(0.0, 1.0).writeln); > > i found out i can't write [0 .. 100] to define a simple number range, but is there a function to do so? Yes! iota(100) > second attempt, replacing the range with an simple array > [0,1,2].map!(v => uniform(0.0,1.0).writeln); > this does compile and run, but doesn't print anything, just an empty string, why is that? Maybe you meant to put the .writeln outside of the parens? Since map is lazily evaluated, writeln is never called and the entire expression does nothing. | |||
January 22, 2015 Re: generate an array of 100 uniform distributed numbers | ||||
|---|---|---|---|---|
| ||||
Posted in reply to ddos | On Thu, 22 Jan 2015 19:26:44 +0000, ddos wrote: > hi guys, firstly this has no direct application, i'm just playing around and learning > > i want to create 100 uniform distributed numbers and print them my first > attempt, just written by intuition: > [0 .. 100].map!(v => uniform(0.0, 1.0).writeln); > > i found out i can't write [0 .. 100] to define a simple number range, but is there a function to do so? The iota function from std.range: iota(0, 100).map!(...) > > second attempt, replacing the range with an simple array [0,1,2].map!(v > => uniform(0.0,1.0).writeln); > this does compile and run, but doesn't print anything, just an empty > string, why is that? Two issues: 1) The function supplied to map should be a projection function, e.g. it takes a value and returns a value. Your lambda returns void (the result of writeln). 2) map is lazy--it doesn't do any work until something consumes it. This is awesome for many reasons (e.g. you can process infinite ranges). Nothing in your code is causing the result of map to be consumed, so it does no work. > finally i got it working with this: > auto t = [0,1,2].map!(v => uniform(0.0,1.0)); > writeln(t); This works because writeln eagerly consumes the result of map, causing the work to actually be done. If you like, you can tack the writeln to the end of the pipeline: auto t = [0,1,2].map!(v => uniform(0.0,1.0)).writeln; | |||
January 22, 2015 Re: generate an array of 100 uniform distributed numbers | ||||
|---|---|---|---|---|
| ||||
Posted in reply to ddos | On 01/22/2015 11:26 AM, ddos wrote:
> i want to create 100 uniform distributed numbers and print them
> my first attempt, just written by intuition:
> [0 .. 100].map!(v => uniform(0.0, 1.0).writeln);
>
> i found out i can't write [0 .. 100] to define a simple number range,
As currently being discussed in another thread, the a..b syntax does not correspond to a first-class D language construct.
> but is there a function to do so?
Yes, std.range.iota:
auto numbers = iota(100).map!(_ => uniform(0.0, 1.0));
Note that, 'numbers' is lazy, the definition above does not call uniform() yet.
To make an array out of it, call std.array.array at the end:
auto numbers = iota(100).map!(_ => uniform(0.0, 1.0)).array;
> second attempt, replacing the range with an simple array
> [0,1,2].map!(v => uniform(0.0,1.0).writeln);
> this does compile and run, but doesn't print anything, just an empty
> string, why is that?
What I said above: it is just a range waiting to be used.
> finally i got it working with this:
> auto t = [0,1,2].map!(v => uniform(0.0,1.0));
> writeln(t);
>
> seems pretty easy eh?
writeln() consumes any range to print it on the standard output. There is no array to speak of though: writeln() consumes a copy of 't' and your 't' is still a lazy range waiting to be consumed.
Ali
| |||
January 22, 2015 Re: generate an array of 100 uniform distributed numbers | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | thx, alot :) works as intended iota(0,100).map!(v => uniform(0.0,1.0)).writeln; | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply