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

Prototype: Headless anonymous function syntax #199

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

c42f
Copy link
Member

@c42f c42f commented Feb 17, 2023

Allow anonymous function syntax like ->_[1] to mean x->x[1] as discussed in JuliaLang/julia#38713

Exists in part of the same design space as #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 c42f changed the title Prototype: Underscores and prefix anonymous function syntax Prototype: Headless anonymous function syntax Feb 17, 2023
c42f added a commit that referenced this pull request Feb 18, 2023
c42f added a commit that referenced this pull request Feb 18, 2023
@c42f
Copy link
Member Author

c42f commented Feb 18, 2023

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

@c42f c42f marked this pull request as draft March 4, 2023 01:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant