JuliaSyntax.jl icon indicating copy to clipboard operation
JuliaSyntax.jl copied to clipboard

Prototype: Headless anonymous function syntax

Open c42f opened this issue 2 years ago • 1 comments

Allow anonymous function syntax like ->_[1] to mean x->x[1] as discussed in https://github.com/JuliaLang/julia/issues/38713

Exists in part of the same design space as https://github.com/JuliaLang/JuliaSyntax.jl/pull/148

You can try this in the REPL by checking out this branch and using JuliaSyntax.enable_in_core!. For example:

julia> JuliaSyntax.enable_in_core!(freeze_world_age=false)

julia> data = [(a=1,b=2), (a=3,b=4)]
2-element Vector{NamedTuple{(:a, :b), Tuple{Int64, Int64}}}:
 (a = 1, b = 2)
 (a = 3, b = 4)

julia> filter(->_.a > 2, data)
1-element Vector{NamedTuple{(:a, :b), Tuple{Int64, Int64}}}:
 (a = 3, b = 4)

With this setup, you can also edit JuliaSyntax.jl to play with different lowering scenarios and see them almost immediately in the REPL using Revise. (May need to run a command twice for Revise to pick things up.)

Note that having lowering implemented here in Expr conversion is just for prototyping/experimentation. It would need to go into julia-syntax.scm in a real implementation.

Meta-commentary

One downside of this proposal (as a kind-of alternative to #148) is that we can't neatly express data filtering operations on rows of tabular data without naming the row, because the row is used multiple times:

julia> filter(row->row.a + row.b > 4, data)
1-element Vector{NamedTuple{(:a, :b), Tuple{Int64, Int64}}}:
 (a = 3, b = 4)

c42f avatar Feb 17 '23 00:02 c42f

I've pushed an extra commit here which makes the _ always refer to the same, single anon function argument.

c42f avatar Feb 18 '23 04:02 c42f