import { Applicative, Applicative1, Applicative2, Applicative2C, Applicative3, } from 'fp-ts/lib/Applicative' import { Foldable, Foldable1, Foldable2, traverse_ } from 'fp-ts/lib/Foldable' import { identity, Predicate } from 'fp-ts/lib/function' import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from 'fp-ts/lib/HKT' import { monoidAll, monoidAny, monoidSum } from 'fp-ts/lib/Monoid' export * from 'fp-ts/lib/Foldable' /** * `and` returns the _conjunction_ of all the `boolean` values in a data * structure. This function will test whether all of the values in a data * structure are `true`. * * @param F Foldable instance * @param bs A foldable container of one or more `boolean`s * * @example * and(remoteData)(initial) //-> true * and(remoteData)(pending) //-> true * and(remoteData)(failure("Oops!")) //-> true * and(remoteData)(failure(true)) //-> true * and(remoteData)(success(false)) //-> false * and(remoteData)(success(true)) //-> true * * and(option)(none) //-> true * and(option)(some(false)) //-> false * and(option)(some(true)) //-> true * * and(array)([false,false,false]) //-> false * and(array)([true,false,false]) //-> false * and(array)([]) //-> true * and(array)([true]) //-> true */ export function and( F: Foldable2, ): (bs: Kind2) => boolean export function and( F: Foldable1, ): (bs: Kind) => boolean export function and(F: Foldable): (bs: HKT) => boolean { return bs => F.foldMap(monoidAll)(bs, identity) } /** * `or` returns the _disjunction_ of a data structure containing one or more * `boolean`s. This function will test whether any of the values in a data * structure is `true`. * * @param F Foldable instance * @param bs A foldable container of one or more `boolean`s * * @example * or(remoteData)(initial) //-> false * or(remoteData)(pending) //-> false * or(remoteData)(failure("Oops!")) //-> false * or(remoteData)(failure(true)) //-> false * or(remoteData)(success(false)) //-> false * or(remoteData)(success(true)) //-> true * * or(option)(none) //-> false * or(option)(some(false)) //-> false * or(option)(some(true)) //-> true * * or(array)([false,false,false]) //-> false * or(array)([true,false,false]) //-> true * or(array)([]) //-> false * or(array)([true]) //-> true */ export function or( F: Foldable2, ): (bs: Kind2) => boolean export function or( F: Foldable1, ): (bs: Kind) => boolean export function or(F: Foldable): (bs: HKT) => boolean { return bs => F.foldMap(monoidAny)(bs, identity) } /** * Tests whether _all_ elements of a data structure satisfy a predicate. * * @param F Foldable instance * @param pred Predicate on type `A` * @param as A foldable container of one or more values of type `A` * */ export function all( F: Foldable2, ): (pred: Predicate, as: Kind2) => boolean export function all( F: Foldable1, ): (pred: Predicate, as: Kind) => boolean export function all( F: Foldable, ): (pred: Predicate, as: HKT) => boolean { return (pred, as) => F.foldMap(monoidAll)(as, pred) } /** * Tests whether _any_ element of a data structure satisfies a predicate. * * @param F Foldable instance * @param pred Predicate on type `A` * @param as A foldable container of one or more values of type `A` * */ export function any( F: Foldable2, ): (pred: Predicate, as: Kind2) => boolean export function any( F: Foldable1, ): (pred: Predicate, as: Kind) => boolean export function any( F: Foldable, ): (pred: Predicate, as: HKT) => boolean { return (pred, as) => F.foldMap(monoidAny)(as, pred) } /** * Counts the number of items that match a predicate in the provided foldable * @param F the Foldable instance * @param pred Predicate on type `A` * @param as A foldable container of one or more values of type `A` */ export function count( F: Foldable2, ): (pred: Predicate) => (as: Kind2) => number export function count( F: Foldable1, ): (pred: Predicate) => (as: Kind) => number export function count( F: Foldable, ): (pred: Predicate) => (as: HKT) => number { return pred => as => F.foldMap(monoidSum)(as, x => (pred(x) ? 1 : 0)) } /** /** * Sequence a data structure, performing some effects encoded by an `Applicative` functor at each value, ignoring the * final result. * * @since 2.0.0 */ export function sequence_( M: Applicative3, F: Foldable1, ): (fa: Kind>) => Kind3 export function sequence_( M: Applicative2, F: Foldable1, ): (fa: Kind>) => Kind2 export function sequence_( M: Applicative2C, F: Foldable1, ): (fa: Kind>) => Kind2 export function sequence_( M: Applicative1, F: Foldable1, ): (fa: Kind>) => Kind export function sequence_( M: Applicative, F: Foldable, ): (fa: HKT>) => HKT export function sequence_( M: Applicative, F: Foldable, ): (fa: HKT>) => HKT { return fa => traverse_(M, F)(fa, identity) }