view general/funutils/fold.m @ 61:eff6bddf82e3 tip

Finally implemented perceptual brightness thing.
author samer
date Sun, 11 Oct 2015 10:20:42 +0100
parents 45aaf9b2d7b0
children
line wrap: on
line source
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.
% 
% Works for any sequence class that has head and next methods,
% including seq(A) and cells(A).

if isempty(x), X=[]; else X=foldl(fn,head(x),next(x)); end