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

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.

Replies are listed 'Best First'.
Re^3: Using lazy list with a for loop
by LanX (Saint) on Jan 03, 2023 at 00:15 UTC
    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.
        > 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