Skip to content

Instantly share code, notes, and snippets.

@lafka
Created September 18, 2014 14:39
Show Gist options
  • Save lafka/a76bd174f7ce2e2d5f65 to your computer and use it in GitHub Desktop.
Save lafka/a76bd174f7ce2e2d5f65 to your computer and use it in GitHub Desktop.
some elixir snippets
defmodule Snippets do
@doc """
## Example:
expr |> either [:ok, {:ok, _}]
"""
defmacro either(match, conds) do
basecond = quote do res -> res end
matches = Enum.reduce conds, basecond, fn(c, acc) ->
[quote do unquote(c) -> :ok end | acc]
end
quote do
case unquote(match) do
unquote(List.flatten matches)
end
end
end
def eithertest(x) do
t |> either [:ok, {:ok, _}]
end
end
:ok = Snippets.eithertest :ok
:ok = Snippets.eithertest {:ok, 1}
:err = Snippets.eithertest :err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment