On 8/20/04, [EMAIL PROTECTED] (Luke Palmer) wrote:
>So all the laziness goes into the array implementation.  But you don't
>even need to write your iterator fancily.  If you just write your scalar
>version of postcircumfix:<>, Perl will do the rest.

So if you use an iterator in list context, Perl will automatically call it 
repeatedly for you?
That still means Perl has to know when something is an iterator, which is 
fine for <> (or whatever it ends up being called), but when your object 
has different ways to iterate (e.g. .keys, .values, .kv), they'd have to 
be defined as "is iterator" or some such.  

...or... you could just do the extra not-so-fancy work yourself.  Multiple 
iterators probably aren't really very common anyway, and it's not that 
much work after all to make your own method that iterates lazily:

>    method postcircumfix:<> ($self: *%opt) returns List {
>        scalar $self.<*%opt>, $self.<*%opt>  # [1]
>    }
>If I write it functionally like that, Perl will still do all the rest.
>Laziness is built right in the language.

Ah, nice!  But doesn't the scalar definition need to be part of the method 
too?  

    method postcircumfix:<> ($self: *%opt) returns Scalar|List  #??
    {
        given want
        {
            when Scalar { get_next_value, increment_counter, etc; }
            when List { return scalar $self.<*%opt>, list $self.<*%opt>; }
            # hm, what about void context?  
            # (should probably iterate once, like scalar context)
        }
    }

Or can you define separate methods for scalar and list contexts, and have 
Perl call whichever one is appropriate?  (Which would be neat... 
polymorphism from the other end.)


                                     -David "polly going crackers" Green

Reply via email to