view general/funutils/@function_handle/decorate.m @ 61:eff6bddf82e3 tip

Finally implemented perceptual brightness thing.
author samer
date Sun, 11 Oct 2015 10:20:42 +0100
parents beb8a3f4a345
children
line wrap: on
line source
% decorate - Decorate function with pre and post operations and optional pausing
%
% decorate :: 
%    (A->B{:})   ~'function from 1 argument to any number of returns', 
%    options {
%       pre  :: (A=>void)/@nop   ~'action to perform before calling function';
%       post :: (A=>void)/@nop   ~'action to perform after calling function'
%    } 
% -> (A->B{:}) ~'new function incorporating pre and post actions'.
function f1=decorate(f,varargin)
	opts=options(varargin{:});
	pf=pauser(opts);
	if isfield(opts,'pre') || isfield(opts,'post')
		pre=getparam(opts,'pre',@nop);
		post=getparam(opts,'post',@nop);
		f1=@f_bracket;
	else
		f1=@f_simple;
	end

	function x=f_simple(x), x=f(x); pf(); end
	function x=f_bracket(x), pre(x); x=f(x); post(x); pf(); end
end