consider this:

  say for map {...} grep {...} map {...} 1..1_000_000

as far as I can imagine, in perl5 it does:
1)flatten 1..1_000_000 into anonimous array; (maybe in this particular case it is optimized in perl5, like it done in C<foreach>.. I don't know.)
2)map trough it elements and store results in another anonimous array of one million elements;
3)then GC probably frees memory of 1..1_000_000
...etc. Please, correct me if I'm wrong.
that's probably fast but not memory efficient. what if after grep we'll have only 1000 elements, but to compute them we used at least 2_000_000 scalar "cells".
apply to it perl6 GC, which wouldn't always free memory immediately, so it could eat 3_000_000 or more.


ok, I know, that 1..n will return an iterator in perl6, which is called only when new item needed. great.

what I want to ask - would map and grep return an iterators too?.. if it's true, then previous construct becames very memory efficient, like if I write
loop ( ... ; ... ; ... ) {...; next if ...; ...; say}


but probably even faster (of course if iterator call wouldn't involve too much overhead). Only one drawback is that now map and grep are forbidden to have "side effects" which is probably a Good Thing.. no one knows when iterators will be called later and in which order.
hm.. It could be a little too functional, though, for perl, which is filled up by side effects. for example, <$filehandle> is iterator too, but it has side effect of changin' position in file. now


 sub process (@a is Lazy) { map {...} @a }

becomes somewhat dangerous if called as

 @b = process <$file>

(assuming we have lazy arrays too. maybe it should be := insted of = ?)
..because it can mess up all successive operations with $file.. (hm.. it will read it all to the eof anyway,so there's little chance that there would be any "successive operations".. but what if next line is "close $file"? I hope you understand what I tried to illustrate. I have no functional programming experience, and I don't know how languages with lazy evaluation avoids problems with files. Maybe it's easy.(?))


ok.. I'm sure it was discussed before, so what you decided?

Reply via email to