The shape every monad in this package has: map and bind.
map
bind
TypeScript has no higher-kinded types, so bind here accepts any Monad<U>; each class narrows it to its own type. Code written against Monad (as Do is) can't stop a caller mixing kinds.
Monad<U>
Monad
Do
import { IO, type Monad } from '@b-jones-rfd/func-help'const m: Monad<number> = IO.of(1).map((n) => n + 1) Copy
import { IO, type Monad } from '@b-jones-rfd/func-help'const m: Monad<number> = IO.of(1).map((n) => n + 1)
The value the monad carries.
Chains a step that itself returns a monad, flattening the result so contexts don't nest. Haskell's >>=.
>>=
The value the returned monad carries.
Applied to the carried value; returns the next monad.
The monad f returns, sequenced after this one.
f
Transforms the carried value, keeping the monad's context.
The type f returns.
Applied to the carried value.
A monad of the same kind carrying f's result.
The shape every monad in this package has:
mapandbind.TypeScript has no higher-kinded types, so
bindhere accepts anyMonad<U>; each class narrows it to its own type. Code written againstMonad(asDois) can't stop a caller mixing kinds.Example