/** * @since 0.24.0 */ import { dual, identity } from "effect/Function" import type { Kind, TypeClass, TypeLambda } from "effect/HKT" import type { Applicative } from "./Applicative.js" /** * @category type class * @since 0.24.0 */ export interface Traversable extends TypeClass { readonly traverse: ( F: Applicative ) => { ( f: (a: A) => Kind ): (self: Kind) => Kind> ( self: Kind, f: (a: A) => Kind ): Kind> } } /** * Returns a default binary `traverse` composition. * * @since 0.24.0 */ export const traverseComposition = ( T: Traversable, G: Traversable ) => (F: Applicative) => ( self: Kind>, f: (a: A) => Kind ): Kind>> => T.traverse(F)(self, G.traverse(F)(f)) /** * Returns a default `sequence` implementation. * * @since 0.24.0 */ export const sequence = (T: Traversable) => (F: Applicative) => ( self: Kind> ): Kind> => T.traverse(F)(self, identity) /** * Given a function which returns a `F` effect, thread this effect * through the running of this function on all the values in `T`, * returning an `T` in a `F` context, ignoring the values * returned by the provided function. * * @since 0.24.0 */ export const traverseTap = (T: Traversable) => (F: Applicative): { ( f: (a: A) => Kind ): (self: Kind) => Kind> ( self: Kind, f: (a: A) => Kind ): Kind> } => dual(2, ( self: Kind, f: (a: A) => Kind ): Kind> => T.traverse(F)(self, (a) => F.map(f(a), () => a)))