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.
  • There's a "reduce" in List::Util, but its interface is fairly horribly broken.

    Anyway, here's a fold-using implementation:


    use strict;

    sub fold {
        my $accum = shift;
        my $code = shift;
        return $accum unless @_;
        return fold( $code->( $accum, shift ), $code, @_ );
    }

    my @files = qw(
            00000.diff
            00001.diff
            00002.diff
            00003.copy
            00004.diff
            00005.diff
            00006.copy
            00007.diff
            00008.diff
    );

    use Data::Dumper;
    sub foldee {
            my ( $accum, $item ) = @_;
            return [$item, []] if ( $item =~ /\.copy$/ );
            return $accum unless defined $accum->[ 0 ];
            return [ $accum->[ 0 ], [@{$accum->[1]}, $item]];
    }
    print Dumper(fold( [undef, []], \&foldee, @files));