% 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
