I find the best way to do Expr manipulation to investigate the expression you have, and the one you want, with dump and then work from there:
julia> dump(:(_ + 2))
Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol +
2: Symbol _
3: Int64 2
julia> dump(:(x -> x + 2))
Expr
head: Symbol ->
args: Array{Any}((2,))
1: Symbol x
2: Expr
head: Symbol block
args: Array{Any}((2,))
1: LineNumberNode
line: Int64 1
file: Symbol REPL[35]
2: Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol +
2: Symbol x
3: Int64 2
Here is a very simple function to do this transformation. It doesn’t handle complicated inputs (e.g. doesn’t recurse into nested Expr etc) but hopefully it should get you started:
julia> function to_func(ex)
# Get a unique symbol for the argument
s = gensym()
# Replace _ with s
# TODO: This doesn't handle e.g. nexted Expr etc and needs refinement
for i in 1:length(ex.args)
if ex.args[i] === :_
ex.args[i] = s
end
end
# Construct return expression
rex = Expr(:->, s, Expr(:block, ex))
return rex
end
to_func (generic function with 1 method)
julia> dump(to_func(:(_ + 2)))
Expr
head: Symbol ->
args: Array{Any}((2,))
1: Symbol ##300
2: Expr
head: Symbol block
args: Array{Any}((1,))
1: Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol +
2: Symbol ##300
3: Int64 2