Skip to content

Instantly share code, notes, and snippets.

@nanne007
Created July 22, 2016 15:34
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nanne007/bd6f3ddf3fb468a7e52ae31f2f07f995 to your computer and use it in GitHub Desktop.
Save nanne007/bd6f3ddf3fb468a7e52ae31f2f07f995 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