Packages

FunLand adds Behaviours to define Algebraic Data Types ('Container' data types) to Elixir, such as Functors, Monoids and Monads.

Current section

Files

Jump to
fun_land lib fun_landic identity.ex
Raw

lib/fun_landic/identity.ex

defmodule FunLandic.Identity do
@moduledoc """
The Identity ADT is a wrapper around a single element.
"""
defstruct [:val]
alias __MODULE__
use FunLand.Monad
# Mappable
def map(%Identity{val: val}, fun) do
wrap(fun.(val))
end
# Appliable
def apply_with(%Identity{val: fun}, %Identity{val: val}) do
wrap(Currying.curry(fun, val))
end
# Applicative
def wrap(val) do
%Identity{val: val}
end
# Chainable
def chain(%Identity{val: val}, fun) do
fun.(val)
end
use FunLand.Reducable
def reduce(%Identity{val: val}, acc, fun) do
fun.(val, acc)
end
# TODO: Traversable
# TODO: Comonad
end