import type * as P from "@principia/prelude"; import * as HKT from "@principia/prelude/HKT"; import { isLeft } from "./guards"; import type { Either, URI, V } from "./model"; /* * ------------------------------------------- * Foldable Either * ------------------------------------------- */ /** * ```haskell * reduce_ :: Foldable f => (f a, b, ((b, a) -> b)) -> b * ``` */ export const reduce_ = (fa: Either, b: B, f: (b: B, a: A) => B): B => (isLeft(fa) ? b : f(b, fa.right)); /** * ```haskell * reduce :: Foldable f => (b, ((b, a) -> b)) -> f a -> b * ``` */ export const reduce = (b: B, f: (b: B, a: A) => B) => (fa: Either): B => reduce_(fa, b, f); /** * ```haskell * foldMap_ :: (Foldable f, Monoid m) => Instance m b -> (f a, (a -> b)) -> b * ``` */ export const foldMap_ = (M: P.Monoid) => (fa: Either, f: (a: A) => M): M => isLeft(fa) ? M.nat : f(fa.right); /** * ```haskell * foldMap :: (Foldable f, Monoid m) => Instance m b -> (a -> b) -> f a -> b * ``` */ export const foldMap = (M: P.Monoid) => (f: (a: A) => M) => (fa: Either): M => foldMap_(M)(fa, f); /** * ```haskell * reduceRight_ :: Foldable f => (f a, b, ((b, a) -> b)) -> b * ``` */ export const reduceRight_ = (fa: Either, b: B, f: (a: A, b: B) => B): B => isLeft(fa) ? b : f(fa.right, b); /** * ```haskell * reduceRight :: Foldable f => (b, ((b, a) -> b)) -> f a -> b * ``` */ export const reduceRight = (b: B, f: (a: A, b: B) => B) => (fa: Either): B => reduceRight_(fa, b, f); /** * @category Instances * @since 1.0.0 */ export const Foldable: P.Foldable<[URI], V> = HKT.instance({ reduce_, foldMap_, reduceRight_, reduce, foldMap, reduceRight });