Skip to content

Instantly share code, notes, and snippets.

@evaporei
Last active November 28, 2025 13:44
Show Gist options
  • Select an option

  • Save evaporei/723ca4a04877ef143d93dcd23878b71c to your computer and use it in GitHub Desktop.

Select an option

Save evaporei/723ca4a04877ef143d93dcd23878b71c to your computer and use it in GitHub Desktop.
Category Theory - Monad Container Implementation
class Container<T> implements Monad<T> {
private value: T
constructor(value: T) {
this.value = value
}
bind<U>(f: (a: T) => Monad<U>): Monad<U> {
return f(this.value)
}
return(a: T): Monad<T> {
return new Container(a)
}
}
console.log(
'bind container with 5 to container with 15:',
new Container(5).bind((a) => new Container(a + 10))
) // Container { value: 15 }
console.log(
'return:',
new Container(undefined).return(6)
) // Container { value: 6 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment