in reply to Efficient but elegant Cross-Product iterator ?

That's exactly what Algorithm::Loops's NestedLoop does.

use Algorithm::Loops qw( NestedLoop ); my $iter = NestedLoop([ map { [ $_->() ] } @subs ]); while ( my @items = $iter->() ) { ... }

Update: LanX points out the above can be simplified if the subs return the same thing each time they are called. They also need to return the values via a reference to an array.

use Algorithm::Loops qw( NestedLoop ); my $iter = NestedLoop(\@subs); while ( my @items = $iter->() ) { ... }

Replies are listed 'Best First'.
Re^2: Efficient but elegant Cross-Product iterator ?
by LanX (Saint) on Jun 20, 2020 at 21:44 UTC
    Almost, instead of code refs, you are expanding the subs to arrays before passing them.

    For this simplified example the result is the same though.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Well yeah. You necessarily need to store the results of the subs somewhere, and in what else would store the results of the subs but in arrays?

        NestedLoops allows to pass code-refs, i.e. they will be dynamically (re-)evaluated each time one loop-level is (re-)entered.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery