how to return map(fn)

steve stevethe1st at gmail.com
Thu Feb 24 08:44:52 UTC 2022


> float times_two(float x) {return 2*x;}
>
> // if you would rather make sure the result is an array
> float[] times_two_array(float[] arr) {
>    import std.algorithm; // for map
>    import std.array; // for array
>    return arr
>       .map!times_two // map your function
>       .array; // convert to an array
> }
>
> void main() {
>    import std.stdio;
>    import std.algorithm; // for map
>    alias map2x = map!times_two;
>    writeln(map2x([1., 2., 3.]));
>    float[] arr2 = times_two_array([1., 2., 3.]);
>    writeln(arr2);
> }
> ```
>
> -Steve

that is exactly what I was trying to achieve thank you so much!



More information about the Digitalmars-d-learn mailing list