Created
July 22, 2016 15:34
-
-
Save nanne007/bd6f3ddf3fb468a7e52ae31f2f07f995 to your computer and use it in GitHub Desktop.
loop, while,break construct in elixir
This file contains hidden or 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 Loop do | |
defmacro while(predicate, do: block) do | |
quote do | |
try do | |
for _ <- Stream.cycle([:ok]) do | |
if unquote(predicate) do | |
unquote(block) | |
else | |
throw :break | |
end | |
end | |
catch | |
:break -> :ok | |
end | |
end | |
end | |
defmacro break, do: throw :break | |
defmacro loop(do: block) do | |
quote do | |
try do | |
for _ < Stream.cycle([:ok]) do | |
unquote(block) | |
end | |
catch | |
:break -> :ok | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment