view general/funutils/fold.m @ 6:0ce3c2070089

Removed duplicate code and fixed doc in timed_action.
author samer
date Mon, 14 Jan 2013 14:33:37 +0000
parents e44f49929e56
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