Casting MapResult

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 15 08:21:47 PDT 2015


On Monday, 15 June 2015 at 15:10:24 UTC, jmh530 wrote:
> So I suppose I have two questions: 1) am I screwing up the 
> cast, or is there no way to convert the MapResult to float[]

Don't cast it, just slap a .array on the end after importing 
std.range. Like so:

import std.algorithm;
import std.range; // add this line somewhere
float[] exp2(float[] x) {
         auto y = x.map!(a => exp(a));
         return y.array; // this line changed to make the array
}


The reason is that map returns a lazy generator instead of an 
array directly. It only evaluates on demand.

To get it to evaluate and save into an array, the .array function 
is called.

Tip though: don't call .array if you don't have to, chaining 
calls to map and such, even foreach(item; some_map_result) can be 
done without actually building the array and can give more 
efficiency.


More information about the Digitalmars-d-learn mailing list