import * as P from "@principia/prelude"; import * as HKT from "@principia/prelude/HKT"; import { bind_, flow } from "../Function"; import { none, some } from "./constructors"; import { Functor, map, map_ } from "./functor"; import { isNone } from "./guards"; import type { Option, URI, V } from "./model"; /* * ------------------------------------------- * Apply Option * ------------------------------------------- */ /** * ```haskell * ap_ :: Apply f => (f (a -> b), f a) -> f b * ``` * * Apply a function to an argument under a type constructor * * @category Apply * @since 1.0.0 */ export const ap_ = (fab: Option<(a: A) => B>, fa: Option): Option => isNone(fab) ? none() : isNone(fa) ? none() : some(fab.value(fa.value)); /** * ```haskell * ap :: Apply f => f a -> f (a -> b) -> f b * ``` * * Apply a function to an argument under a type constructor * * @category Apply * @since 1.0.0 */ export const ap = (fa: Option) => (fab: Option<(a: A) => B>): Option => ap_(fab, fa); export const apFirst_ = (fa: Option, fb: Option): Option => ap_( map_(fa, (a) => () => a), fb ); export const apFirst = (fb: Option) => (fa: Option): Option => apFirst_(fa, fb); export const apSecond_ = (fa: Option, fb: Option): Option => ap_( map_(fa, () => (b: B) => b), fb ); export const apSecond = (fb: Option) => (fa: Option): Option => apSecond_(fa, fb); /** * ```haskell * mapBoth_ :: Apply f => (f a, f b, ((a, b) -> c)) -> f c * ``` * * Applies both `Maybe`s and if both are `Some`, maps their results with function `f`, otherwise returns `Nothing` * * @category Apply * @since 1.0.0 */ export const mapBoth_ = (fa: Option, fb: Option, f: (a: A, b: B) => C): Option => ap_( map_(fa, (a) => (b: B) => f(a, b)), fb ); /** * ```haskell * mapBoth :: Apply f => (f b, ((a, b) -> c)) -> f a -> f c * ``` * * Applies both `Maybe`s and if both are `Some`, maps their results with function `f`, otherwise returns `Nothing` * * @category Apply * @since 1.0.0 */ export const mapBoth = (fb: Option, f: (a: A, b: B) => C) => (fa: Option): Option => mapBoth_(fa, fb, f); /** * ```haskell * liftA2 :: Apply f => (a -> b -> c) -> f a -> f b -> f c * ``` * * Lifts a binary function to actions * * @category Apply * @since 1.0.0 */ export const liftA2 = (f: (a: A) => (b: B) => C) => (fa: Option) => (fb: Option): Option => isNone(fa) ? none() : isNone(fb) ? none() : some(f(fa.value)(fb.value)); /** * ```haskell * apS :: (Apply f, Nominal n) => * (n n3, f c) * -> f ({ n1: a, n2: b, ... }) * -> f ({ n1: a, n2: b, n3: c }) * ``` * * A pipeable version of `sequenceS` * * @category Apply * @since 1.0.0 */ export const apS = ( name: Exclude, fb: Option ): ((fa: Option) => Option<{ [K in keyof A | N]: K extends keyof A ? A[K] : B }>) => flow( map((a) => (b: B) => bind_(a, name, b)), ap(fb) ); export const Apply: P.Apply<[URI], V> = HKT.instance({ ...Functor, ap_, ap, mapBoth_, mapBoth }); export const sequenceS = P.sequenceSF(Apply); export const sequenceT = P.sequenceTF(Apply); export const mapN = P.mapNF(Apply);