import type * as P from "@principia/prelude"; import * as HKT from "@principia/prelude/HKT"; import { right } from "./constructors"; import { isLeft } from "./guards"; import type { Either, URI, V } from "./model"; /* * ------------------------------------------- * Functor Either * ------------------------------------------- */ /** * ```haskell * map_ :: Functor f => (f a, (a -> b)) -> f b * ``` * * Lifts a function a -> b to a function f a -> f b * * @category Functor * @since 1.0.0 */ export const map_ = (fa: Either, f: (a: A) => B): Either => (isLeft(fa) ? fa : right(f(fa.right))); /** * ```haskell * map :: functor f => (a -> b) -> f a -> f b * ``` * * lifts a function a -> b to a function f a -> f b * * @category Functor * @since 1.0.0 */ export const map = (f: (a: A) => B) => (fa: Either): Either => map_(fa, f); /** * @category Instances * @since 1.0.0 */ export const Functor: P.Functor<[URI], V> = HKT.instance({ map, map_ });