R 中的高级函数 - 是否有官方的 compose 运算符或 curry 函数? [英] higher level functions in R - is there an official compose operator or curry function?

查看:11
本文介绍了R 中的高级函数 - 是否有官方的 compose 运算符或 curry 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 R 中创建一个组合运算符:

I can create a compose operator in R:

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

像这样使用:

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

但我想知道是否有一套官方的函数来做这种事情和其他操作,例如在 R 中进行柯里化.主要是为了减少我代码中的括号、函数关键字等的数量.

but I would like to know if there is an official set of functions to do this kind of thing and other operations such as currying in R. Largely this is to reduce the number of brackets, function keywords etc in my code.

我的咖喱函数:

> 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"

这尤其适用于例如汇总:

This is especially nice for e.g. aggregate:

> 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 完成了吗?

Basically I want to know - has this already been done for R?

推荐答案

R 中函数式编程的标准位置现在是 functional 库.

The standard place for functional programming in R is now the functional library.

来自图书馆:

函数式:Curry、Compose 和其他高阶函数

functional: Curry, Compose, and other higher-order functions

示例:

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

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

PS:这个库替代了 ROxigen 库.

PS: This library substitutes the ROxigen library.

这篇关于R 中的高级函数 - 是否有官方的 compose 运算符或 curry 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆