view sequences/@takewhile/takewhile.m @ 0:672052bd81f8

Initial partial import.
author samer
date Wed, 19 Dec 2012 22:38:28 +0000
parents
children
line wrap: on
line source
function o=takewhile(f,source)
% takewhile - Take elements of seq while a condition is met.
%
% takewhile :: (A->bool), seq A -> seq A

if nargin==0, o=takewhile(@(e)true,0);
elseif isa(source,'takewhile'), o=source;
elseif isempty(source), o=[]; 
elseif ~f(head(source)), o=[];
else
	ft.datafn=@datafn;
	ft.stringfn=@stringfn;
	ft.nextfn=@nextfn;
	o.f=f;
	o.x=head(source);
	o=class(o,'takewhile',ddata(source,size(source),ft));
end

function x=datafn(o), x=o.x;
function s=stringfn(o), s=sprintf('takewhile(%s)',tostring(o.f));
function o=nextfn(o)
	o=next_c(o);
	if ~isempty(o), 
		o.x=headsource(o);
		if ~o.f(o.x), o=[]; end
	end