Created
February 23, 2015 15:22
-
-
Save bcj/548b3038b7794dca86b4 to your computer and use it in GitHub Desktop.
Arbitrary call order in Julia
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
function join_function(a, b :: Union(String, Char)) | |
# Julia's is a bit too permissive on types | |
join(a, b) | |
end | |
list = [1, 2, 3] | |
separator = ", " | |
macro call(symbol_a, symbol_b, symbol_c) | |
a, b, c = eval(:( $symbol_a, $symbol_b, $symbol_c)) | |
if isa(a, Function) | |
if method_exists(a, (typeof(b), typeof(c))) | |
return a(b, c) | |
else | |
return a(c, b) | |
end | |
elseif isa(b, Function) | |
if method_exists(b, (typeof(a), typeof(c))) | |
return b(a, c) | |
else | |
return b(c, a) | |
end | |
else # c is the function | |
if method_exists(c, (typeof(a), typeof(b))) | |
return c(a, b) | |
else | |
return c(b, a) | |
end | |
end | |
end | |
println(@call join_function list separator) | |
println(@call join_function separator list) | |
println(@call separator join_function list) | |
println(@call separator list join_function) | |
println(@call list join_function separator) | |
println(@call list separator join_function) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment