Last active
September 6, 2016 23:06
-
-
Save whitfin/7e1af6ea2ba396dfe1e2de35ce1c7f8b to your computer and use it in GitHub Desktop.
If you're working against Elixir v1.1, here's a simple (single tier) with statement
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 MyModule do | |
defmacro exec_with(left, right, do: fun) do | |
quote do | |
case unquote(right) do | |
unquote(left) -> | |
unquote(fun) | |
v -> v | |
end | |
end | |
end | |
end | |
iex(1)> defmodule MyModule do | |
...(1)> defmacro exec_with(left, right, do: fun) do | |
...(1)> quote do | |
...(1)> case unquote(right) do | |
...(1)> unquote(left) -> | |
...(1)> unquote(fun) | |
...(1)> v -> v | |
...(1)> end | |
...(1)> end | |
...(1)> end | |
...(1)> end | |
iex(2)> | |
iex(3)> import MyModule | |
iex(4)> | |
iex(5)> val = exec_with { a, b, c }, { 1, 2, 3 } do | |
...(5)> a + b + c | |
...(5)> end | |
iex(6)> val == 6 | |
iex(7)> | |
iex(8)> val = exec_with { a, b, c }, 1000 do | |
...(8)> a + b + c | |
...(8)> end | |
...(8)> val == 1000 | |
iex(9)> | |
iex(10)> val = exec_with { a, b, c } = res, { 1, 2, 3 } do | |
...(10)> res | |
...(10)> end | |
iex(11)> val == { 1, 2, 3 } | |
iex(12)> | |
iex(13)> val = exec_with { a, b, c }, { 1, 2, 3 }, do: a + b + c | |
iex(14)> val == 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment