UNPKG

@jsoldi/hkt

Version:

Higher kinded types for typescript and a few utility monads.

30 lines 1.11 kB
import { monad } from "../classes/monad.js"; import { monadTrans } from "../classes/transformer.js"; /** Creates a state monad with a fixed state type. */ function stateOf() { const unit = (a) => s => [a, s]; const bind = (fa, f) => s => { const [a, t] = fa(s); return f(a)(t); }; const of = () => stateOf(); const get = s => [s, s]; const put = (s) => _ => [null, s]; const transform = (inner) => { const unit = (a) => s => inner.unit([a, s]); const bind = (fa, f) => s => inner.bind(fa(s), ([a, t]) => f(a)(t)); const lift = (ma) => s => inner.map(ma, a => [a, s]); const wrap = fa => s => inner.unit(fa(s)); return monadTrans({ unit, bind, lift, wrap }); }; return { ...monad({ unit, bind }), of, get, put, transform }; } /** The `state` module, providing a set of functions for working with state functions. The environment type is fixed to `any`. To change the environment type, call the `of` function. */ export const state = stateOf(); //# sourceMappingURL=state.js.map