function o=concat(sources,varargin)
% concat - Concatenate sequences
%
% concat ::
%    seq (seq A)	~ 'sequence of sequences'
% -> seq A       ~ 'resultant sequence'.
%
% concat ::
%    {[N]->seq A}	~ 'cell array of N data objects',
% -> seq A          ~ 'resultant sequence'.


if nargin==0, o=concat(repeat(0));
elseif nargin==1 && isa(sources,'concat'), o=elems;
else
	if iscell(sources), sources=celldata(sources); end
	if isempty(sources), o=[]; return; end

	hd=head(sources);
	while isempty(hd)
		sources=next(sources);
		if isempty(sources), o=[]; return; end
		hd=head(sources);
	end
	o.current=hd;
	o.sources=next(sources);
	ft.datafn=@datafn;
	ft.charfn=@stringfn;
	ft.nextfn=@nextfn;

	o=class(o,'concat',data(size(o.current),ft));
end


function x=datafn(o), x=head(o.current);
function s=stringfn(o), s=sprintf('concat(%s|...)',tostring(o.current));
function o=nextfn(o),
	s1=next(o.current);
	if ~isempty(s1), 
		o.current=s1; 
		o=setsize(o,size(o.current)); 
	else
		if isempty(o.sources), o=[]; return; end

		hd=head(o.sources);
		while isempty(hd)
			o.sources=next(o.sources);
			if isempty(o.sources), o=[]; return; end
			hd=head(o.sources);
		end
		o.current=hd;
		o.sources=next(o.sources);
		o=setsize(o,size(hd)); 
	end
