in reply to Using lazy list with a for loop

> but I would also like to keep the current user interface of simply iteratoring over the results with a for loop. Is this possible?

In order to help you better, we need to know why.

Put differently: Why not a while loop?

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

Replies are listed 'Best First'.
Re^2: Using lazy list with a for loop
by Anonymous Monk on Jan 02, 2023 at 23:58 UTC
    Because it is the user interface. I don't mind changing the implementation details behind the scenes, but I'd like to avoid inconveniencing others if possible. It's also more code- a single for loop has to be replaced by 2 statements, 1 to create the iterator, and the while 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

        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.