[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: |
Tue, 25 Oct 2011 11:34:36 -0700 |
2011/10/14 Jordi Gutiérrez Hermoso <address@hidden>:
> 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 pushed this:
http://hg.savannah.gnu.org/hgweb/octave/rev/62d1f56b0be7
- Jordi G. H.