Created
September 24, 2014 16:56
-
-
Save takscape/5ceb8409c62faa809da4 to your computer and use it in GitHub Desktop.
Maybe
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 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