view sequences/+seq/selector.m @ 61:eff6bddf82e3 tip

Finally implemented perceptual brightness thing.
author samer
date Sun, 11 Oct 2015 10:20:42 +0100
parents 9d24b616bb06
children
line wrap: on
line source
% selector - Use boolean function to keep only matching elements from sequence
%
% selector :: (A->bool), seq(A) -> seq(A).
classdef selector < seq
	properties (GetAccess=private, SetAccess=private)
		fn       % :: A->bool
		source   % :: seq(A)
		x        % :: A
	end
	methods (Static)
		function o=make(fn,source)
			source=dropwhile(inverse(fn),source);
			if isempty(source), o=nil; else o=seq.selector(fn,source); end
		end
	end
	methods
		function d=selector(fn,source)
			d.fn=fn; 	
			d.x=head(source);
			d.source=source;
		end

		function z=elsize(o), z=elsize(d.x); end
		function s=tostring(o), s=sprintf('%s >> %s?',tostring(o.source),tostring(o.fn)); end
		function x=head(d), x=d.x; end
		function o=next(o)
			while 1
				o.source=next(o.source);
				if isempty(o.source), o=nil; return; end
				o.x=head(o.source);
				if o.fn(o.x), break; end
			end
		end
	end
end

function g=inverse(f), g=@(x)~f(x); end