in reply to Designing a DWIMish interface for generator function

Anyhow this is sabotaging performance, since calling methods on objects or ties is quite expensive...

Ties aren't cheap compared to variable lookups because they involve a sub call. However, they shouldn't be much slower than a sub call.

Method calls shouldn't be any slower than an sub call because they do the same thing. Even if inheritance is involved, Perl caches the inherited method so it doesn't have to search the inheritance tree for it again.

Of course, there's no reason to use a method call here. $f->() would work just as well.

for my $i ( range(1,9) ) { print $i }

That would take an efficient counting loop (O(1) memory) into an inefficient foreach loop (O(N) memory)

while ( range(1,9) ) {...}

You could make this work by storing the current state of the counter in a per-thread hash indexed by the operator instance. The problem is that you'd have no way of resetting the counter if you previously used last to exit the loop.

On the other hand, the following would be trivial to implement.

for my $i ( 1..9 ) { print $i } for my $i ( range(1,9) ) { print $i } { my $i; while ( range 1,9 => $i ) {...} } { my $iter = range(1,9); while ( $iter->(my $i) ) {...} }
sub range { my $start = shift; my $end = shift; if (!@_) { if (wantarray()) { return $start .. $end; } else { my $i = $start; return sub { return undef if $i > $end; $_[0] = $i++; return 1; }; } } if (defined($_[0])) { ++$_[0]; } else { $_[0] = $start; } if ($_[0] > $end) { return $_[0] = undef; } else { return 1; } }

Replies are listed 'Best First'.
Re^2: Designing a DWIMish interface for generator function
by LanX (Saint) on Feb 01, 2010 at 04:12 UTC
    I have a pretty clear idea of how to implement it, I'm looking for the best interface resp. usage pattern to distinct between returning single steps and an iterator function.

    But I'm confident that the idea in the update is the best way to go.

    But please why don't you consider using while ( range 1,9 => my $i ) {...} instead of { my $i; while ( range 1,9 => $i ) {...} }?

    Cheers Rolf

      You need something external to the loop to keep the state.

      You could use a combination of the address of the calling opcode as a key to get the appropriate state, but you'd have no way to reset to the counter if you did that, so you wouldn't be able to last/return/die from within the loop.

      Just look at each for example. To properly use each, you need to do:

      keys %hash; # Reset iterator while (my ($k, $v) = each(%hash)) { ... }

      And even then, it doesn't nest because the iterator is per-hash, not per-each.

        Did you have a look at the code I linked to? It keeps the state, by using the ref of the iterator-variable as key:

        use strict; use warnings; { my %lists; sub deal { my $aref=shift; my $id= \$_[0]; #print $aref,$id; $lists{$id}=$aref if (! exists $lists{$id} ); if (@{$lists{$id}}){ for(@_) { $_=shift @{$lists{$id}}; } return 1; } else { delete $lists{$id}; return 0; } } } $\="\n"; #--------- here we go while (deal [1..9] => my ($a,$b,$c) ){ print $a,$b,$c; while (deal [a=>1,b=>2] => my ($a,$b) ){ print $a,$b; } } #--------- print "EXIT"; __DATA__ 123 a1 b2 456 a1 b2 789 a1 b2

        But I have to admit that till now I haven't thought about last, that's indeed a problem...

        Cheers Rolf