Skip to content

Latest commit

 

History

History
189 lines (125 loc) · 3.97 KB

controls.md

File metadata and controls

189 lines (125 loc) · 3.97 KB

controls

module

Contravariant

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:

  1. Identity: (pullback { a => a }, value) == value
  2. Associative: (pipe [pullback f, pullback g], value) == pullback pipe [f, g], value

Properties

  • pullback transform, value

ContravariantWitness

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

Cases

Functor

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:

  1. Identity: (map { a => a }, value) == value
  2. Associative: (pipe [map f, map g], value) == map pipe [f, g], value

Properties

  • map f, value - Transforms a wrapped value using a function depending context of the functor

FunctorWitness

enum Defines all valid witnesses for a functor.

import lists

map incr, lists
map incr, lists.map
map incr, lists.functor

Cases

Monad

data Monads apply a function returning wrapped values to a wrapped value.

Invariants:

  1. Left-Identity: (pipe [pure, flatMap f], value) == f value
  2. Right-Identity: (pipe [pure, flatMap { x => x }], value) == pure value
  3. Associative: (pipe [pure, flatMap f, flatMap g], value) == pipe [pure, flatMap g, flatMap f], value

Properties

  • pure value - Wraps a value in a neutral context.
  • flatMap f, instance - Transforms a wrapped value and merges potential partial results.

MonadWitness

enum Valid witnesses for a monad.

import lists

flatMap repeat 2, lists
flatMap repeat 2, lists.monad

Cases

contravariantFrom

func contravariantFrom moduleWitness

Creates a Contravariant from a given ContravariantWitness.

flatMap

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.

functorFrom

func functorFrom moduleWitness

Creates a Functor from a given FunctorWitness.

map

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]

monadFrom

func monadFrom monadWitness

Creates a Monad from a given MonadWitness.

pullback

func pullback f, witness

pullback for a yet unknown witness.

pure

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.