annotate 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 |
rev |
line source |
samer@4
|
1 function X=fold(fn,args)
|
samer@4
|
2 % fold - Fold combinator from functional programming
|
samer@4
|
3 %
|
samer@4
|
4 % This function applies an associative operator to a list of arguments,
|
samer@4
|
5 % as if the list was written out with the operator between consecutive
|
samer@4
|
6 % elements, eg fold(@plus,{1,2,3,4}) = 1+2+3+4.
|
samer@4
|
7 %
|
samer@4
|
8 % fold ::
|
samer@4
|
9 % (X,X->X) ~'associative binary operator',
|
samer@4
|
10 % {[N]->X} ~'list (cell array) of arguments (at least 2 elements)'
|
samer@4
|
11 % -> X.
|
samer@4
|
12
|
samer@4
|
13 if isempty(args), X=[]; else X=foldl(fn,args{1},args(2:end)); end
|