in reply to Re^2: Using lazy list with a for loop
in thread Using lazy list with a for loop

Merging the semantics of for and while in Perl is very hard. I experimented for years and it always came with a penalty, like performance or fuzzy variable scope.

E.g. you can use syntactic sugar to define 2 subs from(&;@) loop(&) with prototypes to define your own loop construct like

from { LIST | ITER } $a,$b => loop { BODY }

an from would check if ITER was blessed to an iterator class and act accordingly.

But you can guess that this would be slow. I suppose even if implemented in XS it would be slow.

BUT "it is ONE user interface".

Are you willing to pay that price?

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^4: Using lazy list with a for loop
by Anonymous Monk on Jan 03, 2023 at 00:26 UTC
    I think that syntax would be the biggest penalty since it would confuse the users. Performance/runtime isn't an issue since the network latency would overshadow it.
      > I think that syntax would be the biggest penalty since it would confuse the users.

      hence you need to switch to while and add an iterator method ->each to your API °

      while ( my ($widget) = $api->each( $item->{sets}{widgets} ) ) { . +.. }

      the iterator will be exhausted as soon as ->each returns an empty list.

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

      °) like others already suggested

        That has all the problems of the each builtin.

        Better to have a generator creator and user.

        my $iter = $api->get_widgets_iter(); while ( my ( $widget ) = $iter->() ) { ... }
        hence you need to switch to while and add an iterator method ->each to your API °
        while (my ($widget) = $api->each( $item->{sets}{widgets})) { ... }
        That would now be the ideal for me, but don't you need to instantiate the iterator before calling it? Like:
        my $iter = $api->each( $item->{sets}{widgets} ); while (my ($widget) = $iter->()) { ... }
        I wonder if you can combine the intializer with the iterator in a single call and have it rebless itself on the first call. Algorithm::Combinatorics does something similar.