import type * as P from "@principia/prelude"; import * as HKT from "@principia/prelude/HKT"; import * as A from "../Array"; import { Functor } from "./functor"; import type { NonEmptyArray, URI, V } from "./model"; import { unit } from "./unit"; /* * ------------------------------------------- * Monad NonEmptyArray * ------------------------------------------- */ /** * ```haskell * flatten :: Monad m => m m a -> m a * ``` * * Removes one level of nesting from a nested `NonEmptyArray` * * @category Monad * @since 1.0.0 */ export const flatten: (mma: NonEmptyArray>) => NonEmptyArray = A.flatten as any; export const chainWithIndex_: ( ma: NonEmptyArray, f: (i: number, a: A) => NonEmptyArray ) => NonEmptyArray = A.chainWithIndex_ as any; export const chainWithIndex: ( f: (i: number, a: A) => NonEmptyArray ) => (ma: NonEmptyArray) => NonEmptyArray = A.chainWithIndex as any; /** * ```haskell * chain_ :: Monad m => (m a, (a -> m b)) -> m b * ``` * * Composes computations in sequence, using the return value of one computation as input for the next * * @category Monad * @since 1.0.0 */ export const chain_: (ma: NonEmptyArray, f: (a: A) => NonEmptyArray) => NonEmptyArray = A.chain_ as any; /** * ```haskell * chain :: Monad m => (a -> m b) -> m a -> m b * ``` * * Composes computations in sequence, using the return value of one computation as input for the next * * @category Monad * @since 1.0.0 */ export const chain: ( f: (a: A) => NonEmptyArray ) => (ma: NonEmptyArray) => NonEmptyArray = A.chain as any; /** * ```haskell * tap_ :: Monad m => (ma, (a -> m b)) -> m a * ``` * * Composes computations in sequence, using the return value of one computation as input for the next * and keeping only the result of the first * * @category Monad * @since 1.0.0 */ export const tap_: (ma: NonEmptyArray, f: (a: A) => NonEmptyArray) => NonEmptyArray = A.tap_ as any; /** * ```haskell * tap :: Monad m => (a -> mb) -> m a -> m a * ``` * * Composes computations in sequence, using the return value of one computation as input for the next * and keeping only the result of the first * * @category Monad * @since 1.0.0 */ export const tap: (f: (a: A) => NonEmptyArray) => (ma: NonEmptyArray) => NonEmptyArray = A.tap as any; export const Monad: P.Monad<[URI], V> = HKT.instance({ ...Functor, flatten, unit });