samer@36: % binder - sort of monadic bind for sequences. samer@3: % samer@36: % binder :: samer@3: % seq(A) ~ 'the first sequence', samer@3: % (A->seq(A)) ~ 'function to return second sequence given last element of first' samer@3: % -> seq(A) ~ 'resultant sequence'. samer@3: % samer@3: % The resulting sequence consists of the entire sequence represented by the samer@3: % first parameter, followed by the sequence obtained by applying the second samer@23: % parameter to the last element of the first sequence. samer@3: % samer@3: % Example: samer@3: % samer@3: % gather(2,bindcat(cellseq({1,2,3,4}),@(x)take(head(x),0))) samer@3: % samer@3: % ans = 1 2 3 4 0 0 0 0 samer@36: classdef binder < seq samer@3: properties (GetAccess=private, SetAccess=private) samer@3: source samer@3: nfn samer@3: x samer@3: end samer@3: methods samer@36: function o=binder(X,F) samer@3: o.source=X; samer@3: o.nfn=F; samer@3: o.x=head(X); samer@3: end samer@3: samer@3: function z=elsize(o), z=size(o.x); end samer@3: function s=tostring(o), s=sprintf('%s >>= %s',tostring(o.source),tostring(o.nfn)); end samer@3: function x=head(o), x=o.x; end samer@3: function o=next(o), samer@3: o.source=next(o.source); samer@3: if isempty(o.source), o=o.nfn(o.x); samer@3: else o.x=head(o.source); samer@3: end samer@3: end samer@3: end samer@3: end