view general/funutils/fold.m @ 4:e44f49929e56

Adding reorganised general toolbox, now in several subdirectories.
author samer
date Sat, 12 Jan 2013 19:21:22 +0000
parents
children 45aaf9b2d7b0
line wrap: on
line source
function X=fold(fn,args)
% fold - Fold combinator from functional programming
%
% This function applies an associative operator to a list of arguments,
% as if the list was written out with the operator between consecutive
% elements, eg fold(@plus,{1,2,3,4}) = 1+2+3+4.
%
% fold :: 
%    (X,X->X)	~'associative binary operator',
%    {[N]->X} ~'list (cell array) of arguments (at least 2 elements)'
% -> X.

if isempty(args), X=[]; else X=foldl(fn,args{1},args(2:end)); end