Skip to content

Instantly share code, notes, and snippets.

@International
Forked from nanne007/loop.ex
Created September 4, 2016 17:50
Show Gist options
  • Select an option

  • Save International/004903c999c5ffdb874a48fb02c12953 to your computer and use it in GitHub Desktop.

Select an option

Save International/004903c999c5ffdb874a48fb02c12953 to your computer and use it in GitHub Desktop.
loop, while,break construct in elixir
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