function cfnh = curry(fn)
%function cfnh = curry(fn)
%curry function allowing functional programming
%fn is a function handle to curry
%for example, given an add function add(x, y):
%d1 = curry(@add);
%d2 = d1(3);
%z = d2(4);
%  z = 7
%taken from http://xtargets.com/snippets/tag/curry by bradphelan
    cfnh = @cfn;
    N = nargin(fn);
    args = {};
    function r = cfn(varargin)
        if nargin==N
            r = fn(args{:}, varargin{:});
        else
            N = N - nargin;
            args = { args{:} varargin{:} };
            r = @cfn;
        end
    end
end
