Last active
August 29, 2015 14:13
-
-
Save hunterboerner/2d1101bbbb058cdaf4c0 to your computer and use it in GitHub Desktop.
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
loop = fn -> | |
receive do | |
{:whatever} -> | |
# stuff | |
loop.() | |
end | |
end |
chrismccord
commented
Jan 14, 2015
defmodule BreakFun do
def break do
throw :break
end
end
defmodule Receiver do
defmacro rec(do: block) do
quote do: rec(do: unquote(block), after: nil)
end
defmacro rec(do: block, after: aft) do
quote do
import BreakFun
try do
loop = fn recurse ->
receive do: unquote(block), after: unquote(aft)
recurse.(recurse)
end
loop.(loop)
catch
:break ->
end
end
end
def run do
rec do
:ping -> IO.puts "Got ping"
after
3000 -> break
end
end
end
Receiver.run
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment