Andrew Pimlott wrote:
>
> Dave Rolsky wrote:
> > Actually, this doesn't encourage it and we 
> > _still_ need iterate() for sure.  We can have
> > set_map() only be used to create a new set, and
> > iterate() only be used to alter the current set
> > in place.
> 
> To clarify, do you mean that iterate can be used
> to alter the current set in place because you can 
> modify $_[0], or because the return value
> of the callback will replace the current element 
> in the set?  Ie, does
> 
>     $set->iterate( sub { $_[0]->clone->add( days 
=> 1 ) } );
> 
> alter $set?  I think you intend that it would not 
> (in fact, it would have no effect at all), which 
> is what I would choose.  After all, you
> might want to iterate through the set without 
> modifying anything:
> 
>    $set->iterate( sub { print $_[0]->hms, "\n" } );
> 

This will not work. In infinite sets, the 'iterate'
subroutine is _stored_ into the object - it is not
executed immediately.

Printing would be done with 'iterator':

    $iter = $set1->iterator;
    while ( $dt = $iter->next ) {
        print $dt->ymd;
    };

Having both 'iterate' and 'iterator' may be 
confusing. Maybe 'iterate' needs a new name.

> If this is what you mean, then I think that having
> both ->iterate and ->set_map (as well as possibly
> ->set_grep) makes perfect sense.
> 
> You might argue that the last example is better
> done with
> 
>     foreach ($set->as_list) { ... }
> 
> which is a legitimate alternative.  I guess my 
> main point is that ->iterate doesn't sound like it
> should modify the set (again, unless you
> modify $_[0]).  And since modification of $_[0] is
> always available, I think it is better not to 
> update the set using the return value of the
> callback.

How about:

$set2 = $set1->set_map( sub { return $_[0] + $x } );

$det1->set_map_inplace( $_[0]->add( $x ) );

$set2 = $set1->set_grep( sub { $_[0] > $x } );

(set_grep_inplace too?)

- Flavio S. Glock



Reply via email to