Skip to content

Instantly share code, notes, and snippets.

@bcj
Created February 23, 2015 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcj/548b3038b7794dca86b4 to your computer and use it in GitHub Desktop.
Save bcj/548b3038b7794dca86b4 to your computer and use it in GitHub Desktop.
Arbitrary call order in Julia
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