Skip to content
Informatikk notater
GitHubLinkedIn

Monad

  • is a structure that implements bind, a map method that wraps the output in a type
  • is a design pattern in which pipeline implementation are abstracted by wrapping a value in type
  • is a type of functor with extra properties within the bind method
    • Functions being passed in bind calls must have as their return types an instance of the monad being used. The bind method of a monad would then need to “unwrap” that value.
  • is implemented using Object instances with a bind method
  • refactors an imperative implementation into declarative statements
  • effectively simulates mutable state using immutable data

An implementation of a monad is a Maybe type.

class Maybe<T> {
  const value = T | undefined
  // takes T as input and returns T wrapped in Maybe
  function wrap<T>(input:T):Maybe<T> {
    return Maybe(input)
  }
  function bind<T>(
    // takes a wrapped value as input
    input: Maybe<T>, 
    // and function to apply to the value
    transform: (_:T) => Maybe<T>
    ):Maybe<T> {
      if (input === none) {
        return none
      }
      return transform(input.value)
  }
  return
}

Video resources