annotate sequences/@bindcat/bindcat.m @ 2:7357e1dc2ad6

Simplified scheduler library with new schedule representation.
author samer
date Sat, 22 Dec 2012 16:17:51 +0000
parents 672052bd81f8
children
rev   line source
samer@0 1 function o=bindcat(X,F,varargin)
samer@0 2 % bindcat - sort of monadic bind for sequences.
samer@0 3 %
samer@0 4 % bindcat ::
samer@0 5 % data A ~ 'the first sequence',
samer@0 6 % (data A->data A)~ 'function to return second sequence given last element of first'
samer@0 7 % -> data A ~ 'resultant sequence'.
samer@0 8 %
samer@0 9 % The resulting sequence consists of the entire sequence represented by the
samer@0 10 % first parameter, followed by the sequence obtained by applying the second
samer@0 11 % parameter to the last nonempty element of the first sequence.
samer@0 12 %
samer@0 13 % Example:
samer@0 14 %
samer@0 15 % gather(2,bindcat(celldata({1,2,3,4}),@(x)take(head(x),0)))
samer@0 16 %
samer@0 17 % ans = 1 2 3 4 0 0 0 0
samer@0 18
samer@0 19
samer@0 20 if nargin==0, o=bindcat(0,@id);
samer@0 21 elseif nargin==1 && isa(sources,'bindcat'), o=elems;
samer@0 22 else
samer@0 23 if isempty(X), o=F(X); % degenerate case
samer@0 24 else
samer@0 25 o.nfn=F;
samer@0 26 o=class(o,'bindcat',ddata(X,size(X), ...
samer@0 27 'datafn',@datafn,'charfn',@stringfn,'nextfn',@nextfn, ...
samer@0 28 varargin{:}));
samer@0 29 end
samer@0 30 end
samer@0 31
samer@0 32
samer@0 33 function x=datafn(o), x=headsource(o);
samer@0 34 function s=stringfn(o), s=sprintf('%s >>= %s',tostring(source(o)),char(o.nfn));
samer@0 35 function o1=nextfn(o),
samer@0 36 o1=next_c(o);
samer@0 37 if isempty(o1), o1=o.nfn(source(o)); end
samer@0 38
samer@0 39
samer@0 40
samer@0 41