annotate general/funutils/@thunk/thunk.m @ 61:eff6bddf82e3 tip

Finally implemented perceptual brightness thing.
author samer
date Sun, 11 Oct 2015 10:20:42 +0100
parents 5de03f77dae1
children
rev   line source
samer@27 1 % thunk(B@typelist(M)) ~ a thunk is something that eventually yields M outputs
samer@4 2 classdef thunk
samer@4 3 properties (GetAccess=private, SetAccess=immutable)
samer@27 4 head % :: (A@typelist(N) -> B | thunk(B))
samer@27 5 args % :: A@typelist(N)
samer@4 6 end
samer@4 7 methods
samer@4 8 % thunk - create delayed evaluation
samer@4 9 function f=thunk(h,varargin), f.head=h; f.args=varargin; end
samer@4 10 function s=tostring(f), s=sprintf('%s(%s)',tostring(f.head),commalist(map(@tostring,f.args))); end
samer@4 11 function display(f), display([tostring(f),' :: thunk']); end
samer@4 12
samer@4 13 % fforce - Completely force a thunk, if keep evaluating till not a thunk
samer@4 14 function f=fforce(f), while isa(f,'thunk'), f=force(f); end; end
samer@4 15
samer@4 16 function varargout=force(f), [varargout{1:nargout}] = feval(f.head,f.args{:}); end
samer@4 17
samer@4 18 % SUBSREF - Subscript referencing for FUNC objects
samer@4 19 function varargout=subsref(f,S)
samer@4 20 s=S(1);
samer@4 21 switch s.type
samer@4 22 case '()', [varargout{1:nargout}] = feval(f.head,f.args{:});
samer@4 23 end
samer@4 24 end
samer@4 25 end
samer@4 26 end
samer@4 27
samer@4 28 function s=commalist(a)
samer@4 29 if isempty(a), s='';
samer@4 30 elseif length(a)==1, s=a{1};
samer@4 31 else s=[a{1},',',commalist(a(2:end))];
samer@4 32 end
samer@4 33 end