Alexey Trofimenko writes:
>  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}

Yep, they will.

> 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>

Well, perhaps not.  Theoretically, at this point, $file has been read
completely.  It's just that it's lying and it hasn't really.  But if you
try to read again, it should resync and things should work properly.

But indeed there are cases where it is a problem:

    my $x = 2;
    sub mklist () {
        return map { 2 * $_ } 0..10;
    }

    my @list = mklist;
    say @list[0..4];   # 0 2 4 6 8
    $x = 1;
    say @list;         # 0 2 4 6 8 5 6 7 8 9 10

Which is assuredly different from what would happen if it were evaluated
non-lazily.

Fortunately, if you fear such effects, you can use the eager flattener:

    my @list = **mklist;

Which will exhaust the iterator and turn it into a real list
immediately.  Of course, if you do:

    my @list = ** 0...;

You'll be testing your computer's temper.

Luke

> 
> (assuming we have lazy arrays too. maybe it should be := insted of = ?)

I don't actually know whether it should be := .  I expect not; the
laziness may be built into the array value implementation. However, it
would feel cleaner to make it behave as a tied array.  But then you'd
have to declare it tied, which would be a pain.

Luke

Reply via email to