module
- data Contravariant
- enum ContravariantWitness
- data Functor
- enum FunctorWitness
- data Monad
- enum MonadWitness
- func contravariantFrom moduleWitness
- func flatMap f, witness, instance
- func functorFrom moduleWitness
- func map f, witness, value
- func monadFrom monadWitness
- func pullback f, witness
- func pure value, witness
data A contravariant wraps behavior to handle inputs depending on a context. Put simply, a contravariant maps inputs, while a functor maps outputs.
import eq
let personByNameEquatbale = eq.contravariant.pullback { person => person.name }, strict
personByNameEquatbale.equal Person "Alice", Person "Bob"
// > False
Invariants:
- Identity:
(pullback { a => a }, value) == value
- Associative:
(pipe [pullback f, pullback g], value) == pullback pipe [f, g], value
pullback transform, value
enum Defines all valid witnesses for a contravariant.
import cmp
pullback { person => person.name }, cmp
pullback { person => person.name }, cmp.pullback
pullback { person => person.name }, cmp.contravariant
data A functor wraps values in a context and allows different decisions depending on the context.
For example, the types Optional
and List
have functors.
import lists
import optionals
let incr = { i => i + 1 }
lists.functor.map incr, [1, 2, 3]
// > [2, 3, 4]
optionals.functor.map incr, Some 41
// > Some 42
optionals.functor.map incr, None
// > None
Invariants:
- Identity:
(map { a => a }, value) == value
- Associative:
(pipe [map f, map g], value) == map pipe [f, g], value
map f, value
- Transforms a wrapped value using a function depending context of the functor
enum Defines all valid witnesses for a functor.
import lists
map incr, lists
map incr, lists.map
map incr, lists.functor
data Monads apply a function returning wrapped values to a wrapped value.
Invariants:
- Left-Identity:
(pipe [pure, flatMap f], value) == f value
- Right-Identity:
(pipe [pure, flatMap { x => x }], value) == pure value
- Associative:
(pipe [pure, flatMap f, flatMap g], value) == pipe [pure, flatMap g, flatMap f], value
pure value
- Wraps a value in a neutral context.flatMap f, instance
- Transforms a wrapped value and merges potential partial results.
enum Valid witnesses for a monad.
import lists
flatMap repeat 2, lists
flatMap repeat 2, lists.monad
func contravariantFrom moduleWitness
Creates a Contravariant from a given ContravariantWitness.
func flatMap f, witness, instance
Flat map for a yet unknown witness and instance. Can be used in generic contexts, where the witness will be curried.
func functorFrom moduleWitness
Creates a Functor from a given FunctorWitness.
func map f, witness, value
Transforms a wrapped value using a yet unknown functor witness and value. Essentially just uses the map of the given witness, but allows to defer the decision regarding the witness itself.
import lists
let incr = { i => i + 1 }
map incr, lists, [1, 2, 3]
func monadFrom monadWitness
Creates a Monad from a given MonadWitness.
func pullback f, witness
pullback for a yet unknown witness.
func pure value, witness
Creates a pure monad value from a yet unknown witness. Can be used in generic contexts, where the witness will be curried.