Casting MapResult
Baz via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jun 15 12:04:31 PDT 2015
On Monday, 15 June 2015 at 15:10:24 UTC, jmh530 wrote:
> I wrote a simple function to apply map to a float dynamic array
>
> auto exp(float[] x) {
> auto y = x.map!(a => exp(a));
> return y;
> }
>
> However, the type of the result is MapResult!(__lambda2,
> float[]). It seems like some of the things that I might do to a
> float[], I can't do to this type, like adding them together. So
> I tried to adjust this by adding in a cast to float[], as in
>
> float[] exp(float[] x) {
> auto y = x.map!(a => exp(a));
> cast(float[]) y;
> return y;
> }
>
> But I get an error that I can't convert MapResult!(__lambda2,
> float[]) to float[].
>
> 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[],
> 2) should I just not bother with map (I wrote an alternate,
> longer, version that doesn't use map but returns float[]
> properly).
In addition to the other answers you can use
std.algorithm.iteration.each():
---
float[] _exp(float[] x) {
auto result = x.dup;
result.each!(a => exp(a));
return result;
}
---
More information about the Digitalmars-d-learn
mailing list