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
	
		


