Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Why not return []'s which get flattened into the output until undef is returned?

        my @rev = induce {@$_ ? pop @$_ : undef} [1..10];

    But maybe I'm missing something as to why the prototype is &$ instead of &@, or am just wanting a better example? I would think that unfold might be more like:

        my @flat = map({map({$_} @$_)} [1..10],[12..20]);

    or thereabouts - and the 'reverse' example does not clarify that for me.

    • Why not return []’s which get flattened into the output until undef is returned?

      Because then there has to be an escape mechanism for the block to be able to return an actual [] that should be preserved in the output, and the block has to make sure to use it when appropriate.

      But maybe I’m missing something as to why the prototype is &$ instead of &@

      Because unfold (“induce”) takes a single value and inflates it to a list, as opposed to fold (reduce) which takes a list and def

      • Why not return []’s which get flattened into the output until undef is returned?

        Because then there has to be an escape mechanism for the block to be able to return an actual [] that should be preserved in the output, and the block has to make sure to use it when appropriate.

        No, you would not reach inside the returned ref - only flatten it at the first level. It is either [] or undef, so [[]] would be a list of one reference.


            my @power = induce {
                my $r = $_ % 12
                $_ /= 12;
                return $r ? [$r] : undef;
            } 4711;


            my @chunk = induce {
              (length) ? [substr $_, 0, 3, ''] : undef
            } "foobarbaz";

        The return $condition ? [@stuff] : unde