在R中(由于magrittr / dplyr),您现在可以调用不带括号的函数,但可以使用管道将其代替。

这意味着无需编码:

> as.character((sqrt(12)^2)
> as.Date("2014-01-01")


您也可以这样做:

> 12 %>% sqrt %>% .^2 %>% as.character
> "2014-01-01" %>% as.Date


R广泛使用它来编辑数据帧。除了数据框外,我认为此语法可读性强,可用于创建功能脚本。

julia语言是否支持类似的内容?

最佳答案

是的,有两种意义。

首先是|>

12 |> sqrt |> x->x^2 |> string  # 11.999999999999998
using Dates  # needed in 0.3, baked in to 0.4
"2014-01-1" |> d->Date(d,"yyyy-mm-dd") |> year |> iseven  # true


我不会说它非常惯用的Julia(或R,这是使用dplyr在数据帧上执行操作的例外)。 There is a discussion about enhancing this type of thing and making the syntax better.您现在可以使用Lazy.jl做很多整洁的事情!

特别是对于DataFrames,它是一个WIP,但是DataFramesMeta.jlLazy.jl结合使用,可以让您执行dplyrLINQ之类的事情,例如(摘自自述文件):

x_thread = @> begin
    df
    @transform(y = 10 * :x)
    @where(:a .> 2)
    @by(:b, meanX = mean(:x), meanY = mean(:y))
    @orderby(:meanX)
    @select(:meanX, :meanY, var = :b)
end

07-24 09:51
查看更多