Created
September 18, 2014 14:39
-
-
Save lafka/a76bd174f7ce2e2d5f65 to your computer and use it in GitHub Desktop.
some elixir snippets
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
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