Skip to content

Instantly share code, notes, and snippets.

@rlb3
Created October 10, 2012 14:44
Show Gist options
  • Select an option

  • Save rlb3/3866074 to your computer and use it in GitHub Desktop.

Select an option

Save rlb3/3866074 to your computer and use it in GitHub Desktop.
A reduce sub in perl
sub reduce (&;$@) {
my $code = shift;
my $f = sub {
my $base_val = shift;
my $g = sub {
my $val = $base_val;
for (@_) {
local ( $a, $b ) = ( $val, $_ );
$val = $code->( $val, $_ );
}
return $val;
};
@_ ? $g->(@_) : $g;
};
@_ ? $f->(@_) : $f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment