-
-
Save hunterboerner/9763a1a6aac90c53bd5c to your computer and use it in GitHub Desktop.
This file contains 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 MonadExample do | |
def call_me(x) do | |
{:ok, 3} | |
>>= pow(2) # This does 3^2; 9. :( | |
{:ok, p} = {:ok, 3} | |
pow(2, p) # This does 2^3; 8. :) | |
go_monads do | |
p <- {:ok, 3} | |
pow(2, p) | |
end | |
{:ok, p} = {:ok, 3} | |
{:ok, b} = {:error, "haha"} | |
do_something(p, b) # => Kaboom! MatchError | |
go_monads do | |
p <- {:ok, 3} | |
b <- {:error, "haha"} | |
do_something(p, b) | |
end # => {:error, "haha"} | |
end | |
def pow(power, base), do: :math.pow(base, power) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment