Skip to content

Instantly share code, notes, and snippets.

@takscape
Created September 24, 2014 16:56
Show Gist options
  • Save takscape/5ceb8409c62faa809da4 to your computer and use it in GitHub Desktop.
Save takscape/5ceb8409c62faa809da4 to your computer and use it in GitHub Desktop.
Maybe
defmodule Foo do
use Pipe
def maybe({:ok, val}, f), do: f.(val)
def maybe({:bad, _} = lhs, f), do: lhs
def maybe(_, _), do: raise ArgumentError
def dec(num) do
if num > 0 do
{:ok, num-1}
else
{:bad, num}
end
end
def main() do
res = pipe_with &maybe/2,
{:ok, 3} |> dec |> dec |> dec |> dec |> dec
IO.puts(inspect(res))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment