R中的高级功能 - 是否有正式撰写操作员或咖喱功能? [英] higher level functions in R - is there an official compose operator or curry function?
问题描述
`%c%`= function(x,y)function( ...)x(y(...))
用法如下:
> numericNull = is.null%c%数字
> numericNull(myVec)
[2] TRUE FALSE
但我想知道是否有是一组官方的函数来做这种事情和其他操作,例如在R中进行currying。这主要是为了减少我的代码中括号,函数关键字等的数量。
我的咖喱功能:
> curry = function(...){
z1 = z0 = substitute(...); z1 [1] = call(list);
函数(...){do.call(as.character(z0 [[1]]),
as.list(c(eval(z1),list(...))) )}}
> p =咖喱(paste(collapse =))
> p(字母[1:10])
[1]abcdefghij
这是特别适合例如聚合:
> df = data.frame(l = sample(1:3,10,rep = TRUE),t =字母[1:10])
>合计(df $ t,df [l],咖喱(paste(collapse =))%c%toupper)
lx
1 1 ADG
2 2 BCH
3 3 EFIJ
其中我发现比以下更优雅和可编辑:
>集合(df $ t,df [l],函数(x)paste(collapse =,toupper(x)))
lx
1 1 ADG
2 2 BCH
3 3 EFIJ
基本上我想知道 - 这已经为R完成了吗?
函数式编程的标准位置现在是函数
库。
来自库:
功能:Curry,Compose等更高
例子:
library(functional)
newfunc< - Curry(oldfunc,x = 5)
CRAN:
https://cran.r-project。 org / web / packages / functional / index.html
这个库代替 ROxigen
图书馆。
I can create a compose operator in R:
`%c%` = function(x,y)function(...)x(y(...))
To be used like this:
> numericNull = is.null %c% numeric
> numericNull(myVec)
[2] TRUE FALSE
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.
My curry function:
> 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
Which I find much more elegant and editable than:
> aggregate(df$t, df["l"], function(x)paste(collapse="",toupper(x)))
l x
1 1 ADG
2 2 BCH
3 3 EFIJ
Basically I want to know - has this already been done for R?
The standard place for functional programming in R is now the functional
library.
From the library:
functional: Curry, Compose, and other higher-order functions
Example:
library(functional)
newfunc <- Curry(oldfunc,x=5)
CRAN: https://cran.r-project.org/web/packages/functional/index.html
PS: This library substitutes the ROxigen
library.
这篇关于R中的高级功能 - 是否有正式撰写操作员或咖喱功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!