-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Backward looking macros: Feature Request 2.0 #47895
Comments
This is indeed a common usecase, but is already possible with the current macro system without introducing extra syntax with potentially complex rules. julia> using DataPipes
julia> tbl = [(a=1, b=2), (a=3, b=4)]
# regular pipe - macro in front
julia> @p tbl |> filter(_.a > 2) |> map(_.b)
1-element Vector{Int64}:
4
# wrote tbl and later realized that extra steps are needed - macro after tbl before those steps
julia> tbl |> @f filter(_.a > 2) |> map(_.b)
1-element Vector{Int64}:
4 I often use this syntax when working with tables interactively, feels pretty convenient so far.
And as for |
This is not exactly the same though. Here |
Other than providing a chaining syntax à la magrittr, what benefits would such a feature bring? |
Binary operators are nice to type in general I suppose. I'm glad we aren't lisp having to write |
I agree that infix operators are nice. They also necessitate the complexity of operator precedence rules. I can see how infix macro support is useful for magrittr, but that's only because I'm familiar with the chaining problem. I'm too unimaginative to consider how it could be useful in other contexts though. Do you have any examples? |
issue #11608 is worth reading btw. There was a short window of time when |
Surely I must be missing something here — is your preferred calling syntax alternative to |
Overhead is indeed small, and only when you type new code - running it again is instant.
Wrt f(a, b, c, @p tbl |> filter(_.x == 1)) with macro in front vs f(a, b, c, tbl |> @f filter(_.x == 1)) with macro in the middle. That's exactly the same amount of notation. Further, |
No, definitely not! I don't have a concrete syntax proposal. But something like w.r.t. Piping that automatically creates functions. I have a strong preference towards a feature which only transforms expressions, without necessarily creating a new scope, since it allows a lot more flexibility, importantly the ability to create variables inside a macro block which are visible outside of it. |
Sure, I also prefer that. But the minor inconvenience of requiring the macro writer to create a function vs a feature that makes code reading nonlinear and more complicated?.. I would definitely choose the former!
Well, it's possible with the existing piping syntax: # the begin-end expression presumably generated by macro
1:5 |> begin
y = nothing
function (x)
y = filter(>=(3), x)
end
end
# y == [3, 4, 5]
# unless run from toplevel REPL - arguments can be made to make it more consistent But why? The presented usecase is specifically for short pipelines where it's too much hassle interactively to move the cursor in front. Maybe, best to keep such pipes at least side effect-free? |
This has been discussed multiple times on Slack and Discord, but I want to file an issue here so it can be formally tracked.
Something I would really like in Julia is "backwards looking macros", broadly similar to R's
%>%
operator magic. But don't worry, this is not yet another piping thread.I really like Chain.jl and use it all the time. ie.
@chain x f1(a) f2(b)
becomesf2(f1(x, a), b)
. The only problem is that it's hard to work with interactively. Often times I writex
first and then only later realize I need to do something to it. In R, a frequent pattern I have iswhere the last
filter
is added because I realized I only wanted to consider observations wherex == 1
in the operation. But I realized this after I had already been typing.I recognize there is InfixFunctions.jl , but it lacks the same flexibility as metaprogramming.
I do not know how complicated this would be to implement. But my preferred syntax would be something along the lines of
which is exactly like a normal macro, but it captures the preceding element. as well. In this way
behaves exactly the same as
Thank you!
The text was updated successfully, but these errors were encountered: