Skip to content

Instantly share code, notes, and snippets.

@chanshik
Forked from nanne007/loop.ex
Created April 2, 2017 18:56
Show Gist options
  • Save chanshik/52049f0ff3a8b64869f7597f968383fd to your computer and use it in GitHub Desktop.
Save chanshik/52049f0ff3a8b64869f7597f968383fd 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