Why is immutable not possible as a result of a reduce expression?

Michael Engelhardt me at mindcrime-ilab.de
Sat Jan 5 11:52:43 PST 2013


Hi,
just playing around with the functional capabilities of D. One 
concept of the pure functional programming is that variables 
should not be reassigned, so the best(?) way to assure this is 
using immutable:

     immutable auto gen = sequence!("n");
     immutable auto seq = take(gen,10);
     immutable auto filtered = filter!("a % 3 == 0 || a % 5 
==0")(seq);
     immutable auto sum = reduce!("a+b")(filtered);

but the last line gives a compiler error:

Error: template instance 
std.algorithm.reduce!("a+b").reduce!(immutable(FilterResult!(unaryFun,immutable(Take!(immutable(Sequence!("n",Tuple!()))))))) 
error instantiating

It compiles and run as expected if I remove the immutable 
constraint on the reduce expression. So what is happening here? I 
thought reduce will create a new data structure containing the 
resulting values like filter and other operations too? That seems 
not to be the case, maybe someone could give me a deeper 
understanding of this.

Thanks in advance

Michael


More information about the Digitalmars-d-learn mailing list