On Thursday, 7 April 2016 at 07:07:40 UTC, Puming wrote:
Hi:
when I use map with joiner, I found that function in map are
called. In the document it says joiner is lazy, so why is the
function called?
say:
int[] mkarray(int a) {
writeln("mkarray called!");
return [a * 2]; // just for test
}
void main() {
auto xs = [1, 2];
auto r = xs.map!(x=>mkarray(x)).joiner;
}
running this will get the output:
mkarray called!
mkarray called!
I suppose joiner does not consume?
when I actually consume the result by writlen, I get more
output:
mkarray called!
mkarray called!
[2mkarray called!
mkarray called!
, 4]
I don't understand
Apparently it works processing the first two elements at
creation. All the other elements will be processed lazily.
Even when a range is lazy the algorithm still often has to
"consume" one or two starting elements, just to set initial
conditions. It does surprise me that joiner needs to process the
first two, would have to look at the implementation why.