On Friday, 29 November 2019 at 15:30:22 UTC, realhet wrote:
...
Unfortunately function purity is not the answer.
I put a very long calculation into the transform function which
is called from "map!".
And the "filter!" is making the "map!" call my function 2 times:
First for the "filter!" to decide it allows the element or not.
And second when the map! is passing it to the foreach() where I
write the elements out.
Is there a solution for this performance issue?
Using functional programming, my program looks beautiful. But I
need it to be optimal too.
//To be easier to understand, I want to make the following:
foreach(a; input){
auto temp = slowFunction(a);
if(check(temp))
writeln(temp);
}
//But the elegant functional form ->
input.map!slowFunction.filter!(a => check(a)).each!writeln;
//Produces the following unoptimal solution:
foreach(a; input){
if(check(slowFunction(a)))
writeln(slowFunction(a));
}
Is there a functional way to fix this?
Thank you in advance!