[R] R as a programming language
Duncan Murdoch
murdoch at stats.uwo.ca
Wed Nov 7 14:42:51 CET 2007
On 11/7/2007 8:17 AM, Alexy Khrabrov wrote:
> On Nov 7, 2007, at 4:13 PM, Duncan Murdoch wrote:
>
>>> And, still no option processing as in GNU long options, or python
>>> or ruby's optparse.
>>> What's the semantics of parameter passing -- by value or by
>>> reference?
>>
>> By value.
>
> Thanks Duncan! So if I have a huge table t, and the idea was to
> write a function t.xy(t, ...) to select slices of it, will parameter
> passing copying waste forfeit all aesthetic savings from
> refactoring? What I'm dreading is having to explicitly select x and
> y from t,
>
> if (<t has some shape>) {
> plot(t$this, t$that, ...)
> } else if (<t has that shape>) {
> plot(t$smth_else, ...)
> }
>
> -- that way I do refer to parts of t and there's no copying except to
> plot (?), yet if indeed passing parameters by value copies them, one
> would have to refrain from writing functions! Is that the state of
> things?
As long as your function doesn't modify t, no actual copy will be made.
My previous message explained it like this:
> Semantically it acts as a full copy, though there is some internal
> optimization that means the copy won't be made until necessary, i.e. one
> of r or t changes.
This applies to argument passing as well as assignments. Argument
passing is very much like an assignment to a new variable in the local
frame. The only difference is the lazy evaluation: the assignment
won't take place until you use the value of that local variable, and if
you never use it, it won't take place at all.
Selecting slices will create new copies of the slices, but you won't get
a new copy of the full table.
Duncan Murdoch
More information about the R-help
mailing list