Last active
January 8, 2016 06:54
-
-
Save mrkaspa/50e3ce762696df8bdc7e to your computer and use it in GitHub Desktop.
If else built in elixir using macros
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
defmodule My do | |
defmacro ifx(conds, [do: code1, else: code2]) do | |
quote do | |
case unquote conds do | |
true -> unquote code1 | |
_ -> unquote code2 | |
end | |
end | |
end | |
end | |
defmodule Test do | |
require My | |
def demo do | |
a = 10 | |
My.ifx a == 10 do | |
IO.puts "yes" | |
else | |
IO.puts "no" | |
end | |
end | |
end | |
Test.demo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment