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

Adding reorganised general toolbox, now in several subdirectories.
author samer
date Sat, 12 Jan 2013 19:21:22 +0000
parents
children
line wrap: on
line source
function X=foldl(fn,e,args)
% foldl - Fold fom the left combinator 
%
% This function applies an associative operator to cell array,
% starting from the left using the given starting element.
% eg fold(@plus,0,{1,2,3,4}) = ((((0+1)+2)+3)+4).
%
% foldl :: 
%    (X,Y->X)	~'associative binary operator',
%    X         ~'initial element',
%    {[N]->Y} ~'list (cell array) of arguments (at least 2 elements)'
% -> X.
X=e; for i=1:length(args), X=fn(X,args{i}); end