diff sequences/@seq/foldl.m @ 3:3f77126f7b5f

First major revision of sequence library, now using classdef form, STILL A BIT BROKEN!
author samer
date Wed, 09 Jan 2013 22:22:21 +0000
parents
children 03694e5c8365
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sequences/@seq/foldl.m	Wed Jan 09 22:22:21 2013 +0000
@@ -0,0 +1,28 @@
+function x=foldl(fn,e,y,varargin)
+% foldl - Fold combinator for sequences
+%
+% This function applies an associative operator to a list of arguments,
+% starting from the left using the given starting element.
+%
+% foldl :: 
+%    (X,Y->X)	~'associative binary operator',
+%    X         ~'initial element',
+%    seq(Y)    ~'a lazy sequence'
+% -> X.
+
+if nargin<4,
+	x=e; 
+	while ~isempty(y)
+		x=fn(x,head(y));
+		y=next(y);
+	end
+else
+	opts=prefs('quiet',1,varargin{:});
+	x=e; 
+	while ~isempty(y)
+		x=fn(x,head(y));
+		y=next(y);
+		if ~opts.quiet, fprintf('.'); end
+		optpause(opts);
+	end
+end