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

Finally implemented perceptual brightness thing.
author samer
date Sun, 11 Oct 2015 10:20:42 +0100
parents 9e7be347b3a0
children
line wrap: on
line source
% binder - sort of monadic bind for sequences.
%
% binder ::
%    seq(A)				~ 'the first sequence',
%	  (A->seq(A))     ~ 'function to return second sequence given last element of first'
% -> seq(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 element of the first sequence. 
%
% Example:
%
%    gather(2,bindcat(cellseq({1,2,3,4}),@(x)take(head(x),0)))
%
% ans = 1  2  3  4  0  0  0  0
classdef binder < seq
	properties (GetAccess=private, SetAccess=private)
		source
		nfn
		x
	end
	methods
		function o=binder(X,F)
			o.source=X;
			o.nfn=F;
			o.x=head(X);
		end

		function z=elsize(o), z=size(o.x); end
		function s=tostring(o), s=sprintf('%s >>= %s',tostring(o.source),tostring(o.nfn)); end
		function x=head(o), x=o.x; end
		function o=next(o),
			o.source=next(o.source); 
			if isempty(o.source), o=o.nfn(o.x); 
			else o.x=head(o.source);
			end
		end
	end
end