specifying an auto array type

Jay Norwood via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 27 08:42:03 PST 2015


On Sunday, 27 December 2015 at 07:40:55 UTC, Ali Çehreli wrote:
>
> It looks like you need map(), not each():
>
> import std.algorithm;
> import std.typecons;
> import std.array;
>
> void main() {
>     auto a = [ 1, 2 ];
>     auto arr = a.map!(e => tuple(2 * e, e * e)).array;
>
>     static assert(is(typeof(arr) == Tuple!(int, int)[]));
> }
>
> Ali

ok, thanks.  This does work, using the uint i ahead of the map 
statement.

uint i=0;
auto arr = samples[].map!(a => 
tuple!("sample","f1","f2","f3")(i++,f1(a),f2(a),f3(a))).array;
writeln(arr);

===== output
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(0, 
3, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(1, 
6, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(2, 
9, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(3, 
12, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(4, 
15, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(5, 
18, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(6, 
21, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(7, 
24, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(8, 
27, 0.7, 0.8)
Tuple!(int, "sample", ulong, "f1", double, "f2", double, "f3")(9, 
30, 0.7, 0.8)

=============
However, I was trying to use each!, with the intention of then 
moving to parallel processing by samples blocks. My guess is this 
would be more efficient than using parallel map or amap, which 
would parallel process by function application, if I understand 
correctly.

It isn't clear to me from the examples if something like below 
can be rewritten to use the chained calls.

foreach(i, ref elem; taskPool.parallel(samples, 100))



More information about the Digitalmars-d-learn mailing list