代码之家  ›  专栏  ›  技术社区  ›  Alex Brown

R中的高级函数-是否有正式的复合运算符或curry函数?

  •  36
  • Alex Brown  · 技术社区  · 14 年前

    我可以在r中创建compose操作符:

     `%c%` = function(x,y)function(...)x(y(...)) 
    

    使用方法如下:

     > numericNull = is.null %c% numeric
     > numericNull(myVec)
     [2] TRUE FALSE
    

    但是我想知道是否有一组正式的函数来做这类事情以及其他操作,比如在r中进行货币化,这在很大程度上是为了减少代码中括号、函数关键字等的数量。

    我的咖喱功能:

    > curry=function(...){
        z1=z0=substitute(...);z1[1]=call("list");
        function(...){do.call(as.character(z0[[1]]),
                              as.list(c(eval(z1),list(...))))}}
    > p = curry(paste(collapse=""))
    > p(letters[1:10])
    [1] "abcdefghij"
    

    这尤其适用于骨料:

    > df = data.frame(l=sample(1:3,10,rep=TRUE), t=letters[1:10])
    > aggregate(df$t,df["l"],curry(paste(collapse="")) %c% toupper)
      l    x
    1 1  ADG
    2 2  BCH
    3 3 EFIJ
    

    我发现它比:

    > aggregate(df$t, df["l"], function(x)paste(collapse="",toupper(x)))
      l    x
    1 1  ADG
    2 2  BCH
    3 3 EFIJ
    

    基本上我想知道-这已经为R做了吗?

    4 回复  |  直到 6 年前
        1
  •  5
  •   elviejo79    9 年前

    functional

       library(functional)
       newfunc <- Curry(oldfunc,x=5)
    

    https://cran.r-project.org/web/packages/functional/index.html

    ROxigen

        2
  •  28
  •   Shane    14 年前

    the roxygen package see the source code here Byron Ellis's solution on R-Help

    Curry <- function(FUN,...) {
      .orig = list(...);
      function(...) do.call(FUN,c(.orig,list(...)))
    }
    
    Compose <- function(...) {
      fs <- list(...)
      function(...) Reduce(function(x, f) f(x),
                           fs,
                           ...)
    }
    

    Reduce Map Filter

    > library(roxygen)
    > p <- Curry(paste, collapse="")
    > p(letters[1:10])
    [1] "abcdefghij"
    

    Compose

    > Compose(function(x) x[length(x):1], Curry(paste, collapse=""), toupper)(letters)
    [1] "ZYXWVUTSRQPONMLKJIHGFEDCBA"
    

    > aggregate(df[,"t"], df["l"], Compose(Curry(paste, collapse=""), toupper))
      l    x
    1 1  ABG
    2 2 DEFH
    3 3  CIJ
    

    plyr by aggregate

    > library(plyr)
    > ddply(df, .(l), function(df) paste(toupper(df[,"t"]), collapse=""))
      l   V1
    1 1  ABG
    2 2 DEFH
    3 3  CIJ
    
        4
  •  2
  •   cmc    6 年前

    plot(rnorm(1000),rnorm(1000)) data.frame

    > data.frame( rnorm(5), rnorm(5), first=rpois(5,1), second=rbinom(5,1,0.5) )
        rnorm.5. rnorm.5..1 first second
    1  0.1964190 -0.2949770     0      0
    2  0.4750665  0.8849750     1      0
    3 -0.7829424  0.4174636     2      0
    4  1.6551403  1.3547863     0      1
    5  1.4044107 -0.4216046     0      0
    

    Curry <- function(FUN, ...) {
        .orig = match.call()
        .orig[[1]] <- NULL # Remove first item, which matches Curry
        .orig[[1]] <- NULL # Remove another item, which matches FUN
        function(...) {
            .inner = match.call()
            .inner[[1]] <- NULL # Remove first item, which matches Curry
            do.call(FUN, c(.orig, .inner), envir=parent.frame())
        }
    }
    

    match.call ... FUN Curry

    .orig .orig[[1]]<-NULL

    Curry(data.frame, rnorm(5), rnorm(5) )( first=rpois(5,1) , second=rbinom(5,1,0.5) )
    

    envir=parent.frame()