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

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.

Replies are listed 'Best First'.
Re^7: Using lazy list with a for loop
by LanX (Saint) on Jan 03, 2023 at 15:32 UTC
    depending on your use cases, yes.

    for instance you could bind the iterator to the code-line where it is first invoked like available via caller

    the next question is if the iterator needs to be destroyed because you leave the loop via last. otherwise an inner loop wouldn't notice if it is reentered.

    For instance, an (IMHO extreme) requirement would be to properly support redo

    In order to cover all use cases you would need

    for (my $iter = create(...); my ($value) = $iter->get(); $iter->inc() +) { ...BODY... }

    as I said, not easy.

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