#
# This software is Copyright 2005 by Elsevier Inc.  You may use it
# under the terms of the license at http://perl.plover.com/hop/LICENSE.txt .
#



###
### reduce
###

## Chapter 7 section 3

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;
}
