如何在R中实现f#的正向管道操作符?(复制)

时间:2022-04-30 22:29:03

This question already has an answer here:

这个问题已经有了答案:

How can you implement F#'s forward pipe operator in R? The operator makes it possible to easily chain a sequence of calculations. For example, when you have an input data and want to call functions foo and bar in sequence, you can write:

如何在R中实现f#的正向管道操作符?操作人员可以很容易地将一系列计算串联起来。例如,当你有一个输入数据并且想要按顺序调用函数foo和bar时,你可以这样写:

data |> foo |> bar

Instead of writing bar(foo(data)). The benefits are that you avoid some parentheses and the computations are written in the same order in which they are executed (left-to-right). In F#, the operator is defined as follows:

而不是写栏(foo(数据))。这样做的好处是可以避免使用括号,并且计算是按照执行的顺序(从左到右)编写的。在f#中,操作符的定义如下:

let (|>) a f = f a

It would appear that %...% can be used for binary operators, but how would this work?

看起来是%……%可以用于二进制运算符,但是如何工作呢?

3 个解决方案

#1


17  

I don't know how well it would hold up to any real use, but this seems (?) to do what you want, at least for single-argument functions ...

我不知道它会有多大的用处,但这看起来(?)可以做你想做的事,至少对于单参数函数来说……

> "%>%" <- function(x,f) do.call(f,list(x))
> pi %>% sin
[1] 1.224606e-16
> pi %>% sin %>% cos
[1] 1
> cos(sin(pi))
[1] 1

#2


7  

Edit: package now on CRAN. Example included.

编辑:现在包在CRAN上。例子包括在内。

The magrittr package is made for this.

magrittr包是为这个做的。

install.packages("magrittr")

Example:

例子:

iris %>%
  subset(Sepal.Length > 5) %>%
  aggregate(. ~ Species, ., mean)

Also, see the vignette:http://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html It has quite a few useful features if you like the F# pipe, and who doesn't?!

另外,请参阅插图:http://cran.r project.org/web/packages/magrittr/vignettes/magrittr.html如果您喜欢f#管道,那么它有很多有用的特性,谁不喜欢呢?

#3


5  

The problem is that you are talking about entirely different paradigms of calling functions so it's not really clear what you want. R only uses what in F# would be tuple arguments (named in R), so one way to think of it is trivially

问题是你谈论的是调用函数的完全不同的范式,所以你并不清楚你想要什么。R只使用f#中的元组参数(在R中命名),所以一种考虑它的方法是很简单的

fp = function(x, f) f(x)

which will perform the call so for example

哪个将执行调用,例如

> fp(4, print)
[1] 4

This is equivalent, but won't work in non-tupple case like 4 |> f x y because there is no such thing in R. You could try to emulate the F# functional behavior, but it would be awkward:

这是等价的,但是在非tupple的情况下,比如4 |> fxy,这是行不通的,因为在r中没有这样的东西。

fp = function(x, f, ...) function(...) f(x, ...)

That will be always functional and thus chaining will work so for example

这将始终是功能性的,因此链接将发挥作用,例如

> tri = function(x, y, z) paste(x,y,z)
> fp("foo", fp("mar", tri))("bar")
[1] "mar foo bar"

but since R doesn't convert incomplete calls into functions it's not really useful. Instead, R has much more flexible calling based on the tuple concept. Note that R uses a mixture of functional and imperative paradigm, it is not purely functional so it doesn't perform argument value matching etc.

但是由于R没有将不完整的调用转换成函数,所以它并不是很有用。相反,R具有基于tuple概念的更灵活的调用。注意R使用了函数和命令范式的混合,它不是纯粹的函数,所以它不执行参数值匹配等等。

Edit: since you changed the question in that you are interested in syntax and only a special case, just replace fp above with the infix notation:

编辑:既然您更改了您对语法感兴趣的问题,并且只对特殊情况感兴趣,只需用infix符号替换上面的fp:

`%>%` = function(x, f) f(x)
> 1:10 %>% range %>% mean
[1] 5.5

(Using Ben's operator ;))

(使用本的运营商,))

#1


17  

I don't know how well it would hold up to any real use, but this seems (?) to do what you want, at least for single-argument functions ...

我不知道它会有多大的用处,但这看起来(?)可以做你想做的事,至少对于单参数函数来说……

> "%>%" <- function(x,f) do.call(f,list(x))
> pi %>% sin
[1] 1.224606e-16
> pi %>% sin %>% cos
[1] 1
> cos(sin(pi))
[1] 1

#2


7  

Edit: package now on CRAN. Example included.

编辑:现在包在CRAN上。例子包括在内。

The magrittr package is made for this.

magrittr包是为这个做的。

install.packages("magrittr")

Example:

例子:

iris %>%
  subset(Sepal.Length > 5) %>%
  aggregate(. ~ Species, ., mean)

Also, see the vignette:http://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html It has quite a few useful features if you like the F# pipe, and who doesn't?!

另外,请参阅插图:http://cran.r project.org/web/packages/magrittr/vignettes/magrittr.html如果您喜欢f#管道,那么它有很多有用的特性,谁不喜欢呢?

#3


5  

The problem is that you are talking about entirely different paradigms of calling functions so it's not really clear what you want. R only uses what in F# would be tuple arguments (named in R), so one way to think of it is trivially

问题是你谈论的是调用函数的完全不同的范式,所以你并不清楚你想要什么。R只使用f#中的元组参数(在R中命名),所以一种考虑它的方法是很简单的

fp = function(x, f) f(x)

which will perform the call so for example

哪个将执行调用,例如

> fp(4, print)
[1] 4

This is equivalent, but won't work in non-tupple case like 4 |> f x y because there is no such thing in R. You could try to emulate the F# functional behavior, but it would be awkward:

这是等价的,但是在非tupple的情况下,比如4 |> fxy,这是行不通的,因为在r中没有这样的东西。

fp = function(x, f, ...) function(...) f(x, ...)

That will be always functional and thus chaining will work so for example

这将始终是功能性的,因此链接将发挥作用,例如

> tri = function(x, y, z) paste(x,y,z)
> fp("foo", fp("mar", tri))("bar")
[1] "mar foo bar"

but since R doesn't convert incomplete calls into functions it's not really useful. Instead, R has much more flexible calling based on the tuple concept. Note that R uses a mixture of functional and imperative paradigm, it is not purely functional so it doesn't perform argument value matching etc.

但是由于R没有将不完整的调用转换成函数,所以它并不是很有用。相反,R具有基于tuple概念的更灵活的调用。注意R使用了函数和命令范式的混合,它不是纯粹的函数,所以它不执行参数值匹配等等。

Edit: since you changed the question in that you are interested in syntax and only a special case, just replace fp above with the infix notation:

编辑:既然您更改了您对语法感兴趣的问题,并且只对特殊情况感兴趣,只需用infix符号替换上面的fp:

`%>%` = function(x, f) f(x)
> 1:10 %>% range %>% mean
[1] 5.5

(Using Ben's operator ;))

(使用本的运营商,))