import type * as P from "@principia/prelude"; import { none, some } from "./constructors"; import { isNone } from "./guards"; import type { Option } from "./model"; import { getApplySemigroup } from "./semigroup"; /* * ------------------------------------------- * Monoid Option * ------------------------------------------- */ export const getApplyMonoid = (M: P.Monoid): P.Monoid> => ({ ...getApplySemigroup(M), nat: some(M.nat) }); export const getFirstMonoid = (): P.Monoid> => ({ combine_: (x, y) => (isNone(y) ? x : y), combine: (y) => (x) => (isNone(y) ? x : y), nat: none() }); export const getLastMonoid = (): P.Monoid> => ({ combine_: (x, y) => (isNone(x) ? y : x), combine: (y) => (x) => (isNone(x) ? y : x), nat: none() }); export const getMonoid = (S: P.Semigroup): P.Monoid> => { const combine_ = (x: Option, y: Option) => isNone(x) ? y : isNone(y) ? x : some(S.combine_(x.value, y.value)); return { combine_, combine: (y) => (x) => combine_(x, y), nat: none() }; };