import type * as P from "@principia/prelude"; import * as HKT from "@principia/prelude/HKT"; import { identity } from "../Function"; import { none, some } from "./constructors"; import { Functor } from "./functor"; import { isNone } from "./guards"; import type { Option, URI, V } from "./model"; /* * ------------------------------------------- * Extend Option * ------------------------------------------- */ /** * ```haskell * extend_ :: Extend w => (w a, (w a -> b)) -> w b * ``` */ export const extend_ = (wa: Option, f: (wa: Option) => B): Option => (isNone(wa) ? none() : some(f(wa))); /** * ```haskell * extend :: Extend w => (w a -> b) -> w a -> w b * ``` */ export const extend = (f: (wa: Option) => B) => (wa: Option): Option => extend_(wa, f); /** * ```haskell * duplicate :: Extend w => w a -> w (w a) * ``` */ export const duplicate = (wa: Option): Option> => extend_(wa, identity); export const Extend: P.Extend<[URI], V> = HKT.instance({ ...Functor, extend });