octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: functions with 'named' arguments


From: Dupuis
Subject: Re: functions with 'named' arguments
Date: Thu, 3 Apr 2008 11:52:21 -0700 (PDT)



John W. Eaton wrote:
> 
> 
> 
> Does R provide the equivalent of a nargin function?  If not, maybe
> this is the reason?  If it does, then how does it work, and would it
> be compatible with what Octave and Matlab currently do?
> 
> 

Hello John,
sorry for the delay -- a few days of vacation.

I analysed R internals, more specifically the function src/bind.c which
provides the implementation of the c(...) function, a general-purpose
constructor for vectors. The only input argument is the "dotdotdot"
operator, and it returns a vector. Basically similar to "[". 

It works as follows: the internal engine receives a list built the Lipsy
way: a set of nodes, each node made of a header and data. The header
contains:
- a type indicator (float, complex, expression (formula), closure,
environment, ...)
- an optional tag (the intended variable name, the left part of the =
operator)
- pointers to previous and next element
The data is an union of the various types

This list ends with a special node serving as end-of-list marker. The
iteration is quite simple, process the actual node, then its CDR, until the
special marker is encountered. 

With this approach, there is no (or better: I didn't find evidence of)
nargin function, because the list is just processed linearly. The drawback
is that the list length is not known in advance.

As I understand,  with a function like
function RP(x=r*cos(theta), y=r*sin(theta), r=sqrt(x*x+y*y), theta=
atan2(x,y)) {
  return c(x, y, r, theta)
}

Examples of this function calls:
- RP(3, 5) : there are data for two arguments (positional matching), then r
and theta are evaluated.
- RP(r = 1, theta= pi/3) : r and theta are initialised from the elements
with corresponding tags, then x and y are evaluated. 
What is called 'lazy loading' is just delayed evaluation of expressions,
permitting a very concise approach in this case at the level of definition
and default values for the arguments.

Regards

Pascal  

-- 
View this message in context: 
http://www.nabble.com/functions-with-%27named%27-arguments-tp16095584p16468634.html
Sent from the Octave - Maintainers mailing list archive at Nabble.com.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]