Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr like %>% syntax in julia

Tags:

dplyr

julia

In R (thanks to magrittr/dplyr) you can now call functions without brackets but you can pipe them along instead.

This means that instead of coding this:

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

You could also do this:

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

R uses this extensively to edit dataframes. Beyond dataframes, I feel that this syntax is very readable and powerful for creating functional scripts.

Does the julia language have support for something similar?

like image 857
cantdutchthis Avatar asked Feb 02 '15 22:02

cantdutchthis


Video Answer


1 Answers

Yes, in two senses.

So first of all there is |>, e.g.

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

I wouldn't say its very idiomatic Julia though (or R, which the exception of doing operations on a dataframe with dplyr). There is a discussion about enhancing this type of thing and making the syntax better. You can do a lot of neat things with Lazy.jl right now though!

For DataFrames in particular, its a WIP, but there is DataFramesMeta.jl combined with Lazy.jl, which lets you do things like dplyr and LINQ such as (taken from their README):

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
like image 164
IainDunning Avatar answered Oct 01 '22 10:10

IainDunning