octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

reduce() function,


From: Muthiah Annamalai
Subject: reduce() function,
Date: Fri, 05 Jan 2007 16:33:47 +0530

Hello,

This is similar in spirit to the ones found in Python.
Please suggest where I can commit this within Octave-Forge?.

Thanks,
Muthiah




## Copyright (C) 2007 Muthiah Annamalai
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
## 02110-1301  USA

## -*- texinfo -*-
## @deftypefn {Function File} address@hidden =} reduce (@var{function},
@var{sequence},@var{initializer})
## @deftypefnx {Function File} address@hidden =} reduce (@var{function},
@var{sequence})
## Implements the 'reduce' operator like in Lisp, or Python.
## Apply function of two arguments cumulatively to the items of
sequence, 
## from left to right, so as to reduce the sequence to a single value.
For example,
## reduce(@@(x,y)(x+y), [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5).
## The left argument, x, is the accumulated value and the right
argument, y, is the 
## update value from the sequence. If the optional initializer is
present, it is 
## placed before the items of the sequence in the calculation, and
serves as
## a default when the sequence is empty. If initializer is not given and
sequence
## contains only one item, the first item is returned.
##
## @example
##      reduce(@@add,[1:10])
##      @result{} 55
##      reduce(@@(x,y)(x*y),[1:7]) 
##      @result{} 5040  (actually, 7!)
## @end example
## @seealso{map}
## @end deftypefn
##

## Parts of documentation copied from the "Python Library Reference,
V2.5"
##

function rv=reduce(func,lst,init)
        if (nargin < 2) || (class(func)!='function_handle') || (nargin == 2 &&
length(lst)<2)
                print_usage();
        end

        l=length(lst);
        if(l<2 && nargin==3)
                if(l==0)
                        rv=init;
                elseif (l==1)
                        rv=func(init,lst(1));
                end
                return;
        end
        
        if(nargin == 3)
                rv=func(init,lst(1));
                start=2;
        else
                rv=func(lst(1),lst(2));
                start=3;
        end
        
        for i=start:l
                rv=func(rv,lst(i));
        end
        return;
end
%!
%!assert(reduce(@(x,y)(x+y),[],-1),-1)
%!assert(reduce(@(x,y)(x+y),[+1],-1),0)
%!assert(reduce(@(x,y)(x+y),[-10:-1]),-55)
%!assert(reduce(@(x,y)(x+y),[-10:-1],+55),0)
%!assert(reduce(@(x,y)(y*x),[1:4],5),120)
%!

Attachment: reduce.m
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]