Skip to content

Instantly share code, notes, and snippets.

@developerworks
Forked from zabirauf/ROP.ex
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developerworks/3b98dcc0f20e5f1ef0cf to your computer and use it in GitHub Desktop.
Save developerworks/3b98dcc0f20e5f1ef0cf to your computer and use it in GitHub Desktop.
defmodule ROP do
defmacro try_catch(args, func) do
quote do
(fn ->
try do
unquote(args) |> unquote(func)
rescue
e -> {:error, e}
end
end).()
end
end
defmacro tee(args, func) do
quote do
(fn ->
unquote(args) |> unquote(func)
{:ok, unquote(args)}
end).()
end
end
defmacro bind(args, func) do
quote do
(fn ->
result = unquote(args) |> unquote(func)
{:ok, result}
end).()
end
end
defmacro left >>> right do
quote do
(fn ->
case unquote(left) do
{:ok, x} -> x |> unquote(right)
{:error, _} = expr -> expr
end
end).()
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment