Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make $x syntactic sugar for _.x #294

Open
davidanthoff opened this issue Jan 18, 2020 · 4 comments
Open

Make $x syntactic sugar for _.x #294

davidanthoff opened this issue Jan 18, 2020 · 4 comments
Milestone

Comments

@davidanthoff
Copy link
Member

I'm not sure this is really a good idea, but I'd like to get some feedback :) The idea would be that $x is just syntactic sugar for _.x in the standalone query commands. So instead of writing:

data |> @mutate(x = 2*_.x)

one could write

data |> @mutate(x = 2 * $x)

The benefit would be that one saves on character.

@davidanthoff davidanthoff added this to the Backlog milestone Jan 18, 2020
@dgkf
Copy link

dgkf commented Feb 9, 2020

I would really like to see some syntactic sugar for _.x.

I think $ is nice in that with data manipulations it's almost analogous to interpolation. However, it being almost the same as interpolation in strings, commands or expressions does introduce some confusion about what a user should expect from this operator.

I particularly like DataFramesMeta.jl approach by using Symbols in that it is (in most cases) quite apparent that the code would cause an error if run as-is. It's fairly self-evident that it's not behaving as you'd typically expect.

In this case, I think it's better to be obvious that the syntax doesn't conform to other paradigms of the language than it is to try to find an almost analogous behavior to map it to.


A bit of brainstorming on the topic: Running with the Symbol syntax, perhaps $ would be better suited for interpolating expressions such that you could programmatically access columns similar to R's rlang::`!!` and it's wrapper tidyeval. (some dplyr examples in the tidyeval cookbook)

Interpolating Symbols

y = :x
data |> @mutate(x = 2 * $y)  # analogous to `rlang::!!`

equal to

data |> @mutate(x = 2 * :x)

Interpolating Expressions

y = :(2 * :x)
data |> @mutate(x = $y) 

equal to

data |> @mutate(x = 2 * :x)

That said, I can't think of a way of implementing something like this as a macro step off the top of my head - maybe with a custom constructor for these data-specific expressions.

@dgkf
Copy link

dgkf commented Feb 10, 2020

Perhaps I'm missing something, but I think $ used outside an expression, string or command throws a syntax error before it can even be intercepted by a macro.

julia> macro test_dollar(e)
           println(e)
           e
       end

julia> y = :x
julia> test_dollar($y)
# ERROR: syntax: "$" expression outside quote
# Stacktrace:
#  [1] top-level scope at REPL[3]:1

@Hasnep
Copy link

Hasnep commented Feb 27, 2020

Coming from R and the Tidyverse, I saw the way that DataFrames.jl uses Symbols, e.g. when joining:

join(people, jobs, on = :ID)

or reshaping:

stack(iris, [:SepalLength, :SepalWidth, :PetalLength, :PetalWidth])

So I was slightly confused by using _., as Symbols already seemed like a good fit and were consistent with other packages.

It was one of the reasons I started learning DataFramesMeta.jl instead of Query.jl. Using _. reminded me of the Python packages that try to bend Python's syntax to be more like dplyr.

@ValdarT
Copy link

ValdarT commented May 5, 2020

Symbol would also be consistent with VegaLite.jl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants