diff sequences/@bindcat/bindcat.m @ 0:672052bd81f8

Initial partial import.
author samer
date Wed, 19 Dec 2012 22:38:28 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sequences/@bindcat/bindcat.m	Wed Dec 19 22:38:28 2012 +0000
@@ -0,0 +1,41 @@
+function o=bindcat(X,F,varargin)
+% bindcat - sort of monadic bind for sequences.
+%
+% bindcat ::
+%    data A				~ 'the first sequence',
+%	  (data A->data A)~ 'function to return second sequence given last element of first'
+% -> data A          ~ 'resultant sequence'.
+%
+% The resulting sequence consists of the entire sequence represented by the
+% first parameter, followed by the sequence obtained by applying the second
+% parameter to the last nonempty element of the first sequence. 
+%
+% Example:
+%
+%    gather(2,bindcat(celldata({1,2,3,4}),@(x)take(head(x),0)))
+%
+% ans = 1  2  3  4  0  0  0  0
+
+
+if nargin==0, o=bindcat(0,@id); 
+elseif nargin==1 && isa(sources,'bindcat'), o=elems;
+else
+	if isempty(X), o=F(X); % degenerate case
+	else
+		o.nfn=F;
+		o=class(o,'bindcat',ddata(X,size(X), ...
+			'datafn',@datafn,'charfn',@stringfn,'nextfn',@nextfn, ...
+			varargin{:}));
+	end
+end
+
+
+function x=datafn(o), x=headsource(o);
+function s=stringfn(o), s=sprintf('%s >>= %s',tostring(source(o)),char(o.nfn));
+function o1=nextfn(o),
+	o1=next_c(o); 
+	if isempty(o1), o1=o.nfn(source(o)); end
+	
+		
+
+