gusl: (Default)
[personal profile] gusl
R's assignment operator is "<-". Typically, R functions do not have side-effects... whenever you refer to an outside variable inside a function, what you're seeing is a copy, shadowing the original... which means that you should return all the information you want to keep.

However, I just discovered that there is a superassignment operator, "<<-", which enables side-effects.

> m <- 5
> f <- function(){ m <<- 10 }
> 
> m
[1] 5
> f()
> m
[1] 10

(no subject)

Date: 2009-10-01 07:46 pm (UTC)
From: [identity profile] gwillen.livejournal.com
The lack of any clear distinction between global and local variables (since any variable can be modified in a global fashion by using <<-) means that any code making significant use of <<- is going to be totally unreadable. It's not clear to me that your characterization of it as 'enabling side-effects' is the correct one, but I would need to see more code samples; from your usage it looks like it just modifies variables as though they were global.

(no subject)

Date: 2009-10-01 07:19 pm (UTC)
From: [identity profile] gustavolacerda.livejournal.com
Copying some objects can be very expensive...

You can still write your code to be side-effect-free, and easily verify it by searching for "<<-".

(no subject)

Date: 2009-10-02 07:26 am (UTC)
From: [identity profile] roseandsigil.livejournal.com
Yes, but, if your code is all functional, you don't need to copy objects, just pointers.

(Also, re: top level post: "arrrrrgghhhhhj, EVERYTHING IS RUINED FOREVER." Like gwillen's reaction, only a billion times worse.)

(no subject)

Date: 2009-10-01 08:59 pm (UTC)
From: [identity profile] trufflesniffer.livejournal.com
how does R compare to LISP, in your view? I've used R fairly extensively (not for last few months though) but never used LISP, though I hear R is based on LISP.

(no subject)

Date: 2009-10-01 09:07 pm (UTC)
From: [identity profile] gustavolacerda.livejournal.com
Uhm, not as elegant in terms of semantics, very Stats-oriented with syntax like ~ |. I think there are no macros (i.e. code that writes code) in R.

I often find the list vs vector distinction annoying, and many commands are named funny. e.g. 'assert' is 'stopifnot'.

Somewhat to my surprise, I've found a few places where it's less annoying than Matlab, e.g. the ability to write things like: f(5)[1].

February 2020

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags