diff general/funutils/fold.m @ 9:45aaf9b2d7b0

Moved high-order sequence/list functions to @cell class.
author samer
date Mon, 14 Jan 2013 15:49:04 +0000
parents e44f49929e56
children
line wrap: on
line diff
--- a/general/funutils/fold.m	Mon Jan 14 14:54:10 2013 +0000
+++ b/general/funutils/fold.m	Mon Jan 14 15:49:04 2013 +0000
@@ -1,13 +1,13 @@
-function X=fold(fn,args)
-% fold - Fold combinator from functional programming
+function X=fold(fn,x)
+% fold - polypmorphic Fold combinator 
+%
+% fold :: (X,X->X), Type(A) -> X :- sequence_class(Type).
 %
 % 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.
+% 
+% Works for any sequence class that has head and next methods,
+% including seq(A) and cells(A).
 
-if isempty(args), X=[]; else X=foldl(fn,args{1},args(2:end)); end
+if isempty(x), X=[]; else X=foldl(fn,head(x),next(x)); end