samer@0: function o=bindcat(X,F,varargin) samer@0: % bindcat - sort of monadic bind for sequences. samer@0: % samer@0: % bindcat :: samer@0: % data A ~ 'the first sequence', samer@0: % (data A->data A)~ 'function to return second sequence given last element of first' samer@0: % -> data A ~ 'resultant sequence'. samer@0: % samer@0: % The resulting sequence consists of the entire sequence represented by the samer@0: % first parameter, followed by the sequence obtained by applying the second samer@0: % parameter to the last nonempty element of the first sequence. samer@0: % samer@0: % Example: samer@0: % samer@0: % gather(2,bindcat(celldata({1,2,3,4}),@(x)take(head(x),0))) samer@0: % samer@0: % ans = 1 2 3 4 0 0 0 0 samer@0: samer@0: samer@0: if nargin==0, o=bindcat(0,@id); samer@0: elseif nargin==1 && isa(sources,'bindcat'), o=elems; samer@0: else samer@0: if isempty(X), o=F(X); % degenerate case samer@0: else samer@0: o.nfn=F; samer@0: o=class(o,'bindcat',ddata(X,size(X), ... samer@0: 'datafn',@datafn,'charfn',@stringfn,'nextfn',@nextfn, ... samer@0: varargin{:})); samer@0: end samer@0: end samer@0: samer@0: samer@0: function x=datafn(o), x=headsource(o); samer@0: function s=stringfn(o), s=sprintf('%s >>= %s',tostring(source(o)),char(o.nfn)); samer@0: function o1=nextfn(o), samer@0: o1=next_c(o); samer@0: if isempty(o1), o1=o.nfn(source(o)); end samer@0: samer@0: samer@0: samer@0: