Created
August 31, 2016 21:04
-
-
Save kreas/865b4658c1a1ad3507481c7522cb9e66 to your computer and use it in GitHub Desktop.
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 Just do | |
defstruct value: nil | |
end | |
defmodule Nothing do | |
defstruct value: nil | |
end | |
defmodule Maybe do | |
defstruct id: :nothing, value: nil | |
import Just | |
import Nothing | |
def init(value \\ nil) | |
def init(value) when value == nil, do: %Nothing{} | |
def init(value), do: %Just{value: value} | |
end | |
defprotocol Monad do | |
def bind(ma, func) | |
end | |
defimpl Monad, for: Just do | |
def bind(%Just{value: value}, func) do | |
%Just{value: func.(value)} | |
end | |
end | |
defimpl Monad, for: Nothing do | |
def bind(_val, _func), do: %Nothing{} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment