Skip to content

Instantly share code, notes, and snippets.

@kreas
Created August 31, 2016 21:04
Show Gist options
  • Save kreas/865b4658c1a1ad3507481c7522cb9e66 to your computer and use it in GitHub Desktop.
Save kreas/865b4658c1a1ad3507481c7522cb9e66 to your computer and use it in GitHub Desktop.
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