[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: nthoutarg function?
From: |
Jordi Gutiérrez Hermoso |
Subject: |
Re: nthoutarg function? |
Date: |
Fri, 14 Oct 2011 11:14:03 -0500 |
2011/10/14 John W. Eaton <address@hidden>:
> On 14-Oct-2011, Jordi Gutiérrez Hermoso wrote:
>
> | This blog post got me thinking:
> |
> | http://abandonmatlab.wordpress.com/2010/05/28/multiple-output-arguments/
> |
> | The blogger is quite right that multiple output arguments are a
> | problem. They require temporaries, and this makes for some slightly
> | brittle code.
> |
> | It's not difficult to implement a generalisation of the
> | "2nd_output_of" function:
> |
> | function out = nthoutarg(fun, n, varargin)
> | outargs = cell (1,n);
> | [outargs{1:end-1}, out] = fun (varargin{:});
> | endfunction
> |
> | Should this function be part of Octave? I'm all for prettifying the
> | language as much as possible.
>
> Because the value of nargout can change the behavior of a function,
> you might need to specify the number of the argument you want and
> the value of nargout to pass to the function separately. For
> example, when calling functions like svd. With your definition of
> nthoutarg,
>
> nthoutarg (@svd, 2, rand (3, 4))
>
> fails. You need something like
>
> function out = nthoutarg (fun, n, nout, varargin)
> if (nargin > 2 && nout >= n)
> outargs = cell (1, nout);
> [outargs{:}] = fun (varargin{:});
> out = outargs{n};
> else
> print_usage ();
> endif
> endfunction
Right, something like it, except it would be nice to make nout == n by
default. I think you would have to change the order of the arguments,
so that
nthargout(n, nout, @fun, args{:})
nthargout(n, @fun, args{:})
could be the two calling forms.
Is this function worth it?
- Jordi G. H.