24

Is it possible to use currying in R?

One possibility is to have special paste functions (it can be considered as a follow up to here), e.g. (in incorrect code):

'%+%' <- (sep)function(x,y) paste(x,y,sep=sep)
"a"%+%("")"b"%+%("_")"c" #gives "ab_c"

What would be a possible implementation in R?

PS: The paste is just an example, I am curious about the possibilities of R...

Community
  • 1
  • 1
teucer
  • 6,060
  • 2
  • 26
  • 36
  • 1
    http://stackoverflow.com/questions/2228544/higher-level-functions-in-r-is-there-an-official-compose-operator-or-curry-func higher level functions in R - is there an official compose operator or curry function? – rcs Mar 18 '11 at 15:45
  • 1
    The proto package can do currying within the context of proto objects. For example, if `p` is a proto object then `p$ls` is `ls` with `p` inserted into the first slot and `p$ls()` runs it. In fact `p$ls()` is the same as `ls(p)` except that it looks for `ls` in `p` and if not found looks into the ancestors of `p`. – G. Grothendieck Mar 18 '11 at 17:27

2 Answers2

28

The standard place for functional programming in R is now the functional library, this library substitutes the ROxigen library that is discussed here :

library(functional)
newfunc <- Curry(oldfunc,x=5)
Community
  • 1
  • 1
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
  • 4
    The Curry function mentioned here is not currying. It's doing partial application. Followed this answer to the functional library, so I thought it was worth putting up a warning ;). – machow Oct 12 '15 at 19:52
2

It is possible to curry in R, and there is a definition in the ROxygen package. See the discussion here

Community
  • 1
  • 1
Kevin L.
  • 1,663
  • 1
  • 16
  • 21