view general/funutils/foldr.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
line wrap: on
line source
function X=foldr(fn,e,args)
% foldr - Fold combinator from functional programming
%
% This function applies an associative operator to a list of arguments,
% starting from the right, eg
% elements, eg foldr(@plus,0,{1,2,3,4}) = (1+(2+(3+(4+0)))).
%
% foldr :: 
%    (X,X->X)	~'associative binary operator',
%    X         ~'initial element',
%    {[N]->X} ~'list (cell array) of arguments (at least 2 elements)'
% -> X.
X=e; for i=length(args):-1:1, X=fn(args{i},X); end