samer@36: % selector - Use boolean function to keep only matching elements from sequence samer@3: % samer@36: % selector :: (A->bool), seq(A) -> seq(A). samer@36: classdef selector < seq samer@3: properties (GetAccess=private, SetAccess=private) samer@23: fn % :: A->bool samer@23: source % :: seq(A) samer@23: x % :: A samer@23: end samer@23: methods (Static) samer@23: function o=make(fn,source) samer@23: source=dropwhile(inverse(fn),source); samer@38: if isempty(source), o=nil; else o=seq.selector(fn,source); end samer@23: end samer@3: end samer@3: methods samer@36: function d=selector(fn,source) samer@23: d.fn=fn; samer@23: d.x=head(source); samer@3: d.source=source; samer@3: end samer@3: samer@3: function z=elsize(o), z=elsize(d.x); end samer@3: function s=tostring(o), s=sprintf('%s >> %s?',tostring(o.source),tostring(o.fn)); end samer@3: function x=head(d), x=d.x; end samer@3: function o=next(o) samer@3: while 1 samer@3: o.source=next(o.source); samer@23: if isempty(o.source), o=nil; return; end samer@3: o.x=head(o.source); samer@3: if o.fn(o.x), break; end samer@3: end samer@3: end samer@3: end samer@3: end samer@23: samer@23: function g=inverse(f), g=@(x)~f(x); end