Created
September 1, 2018 01:20
-
-
Save solson/71f2d491e6809dbbea74e4460ef0508f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module FnMacro | |
export @fn | |
arg_name(n::Int) = Symbol("hole_", n) | |
rewrite(x, n::Ref{Int}) = x | |
rewrite(s::Symbol, n::Ref{Int}) = s == :_ ? arg_name(n[] += 1) : s | |
rewrite(e::Expr, n::Ref{Int}) = Expr(e.head, map(a -> rewrite(a, n), e.args)...) | |
macro fn(e) | |
num_args = Ref(0) | |
body = rewrite(e, num_args) | |
arg_names = map(arg_name, 1:num_args[]) | |
:(($(arg_names...),) -> $body) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment