// ets_tracing: off /** * A `Traversal` is the generalisation of an `Optional` to several targets. In other word, a `Traversal` allows to focus * from a type `S` into `0` to `n` values of type `A`. * * The most common example of a `Traversal` would be to focus into all elements inside of a container (e.g. * `Array`, `Option`). To do this we will use the relation between the typeclass `ForEach` and `Traversal`. */ import * as A from "@effect-ts/core/Collections/Immutable/Array" import * as L from "@effect-ts/core/Collections/Immutable/List" import * as C from "@effect-ts/core/Const" import type { Either } from "@effect-ts/core/Either" import type { Predicate, Refinement } from "@effect-ts/core/Function" import { identity, pipe } from "@effect-ts/core/Function" import * as I from "@effect-ts/core/Id" import type { Identity } from "@effect-ts/core/Identity" import type { Option } from "@effect-ts/core/Option" import type { URI } from "@effect-ts/core/Prelude" import * as P from "@effect-ts/core/Prelude" import type { HashMap } from "@effect-ts/system/Collections/Immutable/HashMap" import * as _ from "../Internal/index.js" import { ModifyF, Traversal } from "../Internal/index.js" // ------------------------------------------------------------------------------------- // model // ------------------------------------------------------------------------------------- export { ModifyF, Traversal } // ------------------------------------------------------------------------------------- // constructors // ------------------------------------------------------------------------------------- export const id = (): Traversal => new Traversal({ modifyF: (F: P.Applicative) => ( f: (a: S) => P.Kind ): ((s: S) => P.Kind) => f }) /** * Create a `Traversal` from a `ForEach` */ export const fromForEach = _.fromForEach // ------------------------------------------------------------------------------------- // compositions // ------------------------------------------------------------------------------------- /** * Compose a `Traversal` with a `Traversal` */ export const compose: ( ab: Traversal ) => (sa: Traversal) => Traversal = _.traversalComposeTraversal // ------------------------------------------------------------------------------------- // combinators // ------------------------------------------------------------------------------------- export const modify = (f: (a: A) => A) => (sa: Traversal): ((s: S) => S) => { return sa.modifyF(I.Applicative)(f) } export const set = (a: A): ((sa: Traversal) => (s: S) => S) => { return modify(() => a) } export function filter( refinement: Refinement ): (sa: Traversal) => Traversal export function filter( predicate: Predicate ): (sa: Traversal) => Traversal export function filter( predicate: Predicate ): (sa: Traversal) => Traversal { return compose(_.prismAsTraversal(_.prismFromPredicate(predicate))) } /** * Return a `Traversal` from a `Traversal` and a prop */ export const prop = ( prop: P ): ((sa: Traversal) => Traversal) => compose(pipe(_.lensId(), _.lensProp(prop), _.lensAsTraversal)) /** * Return a `Traversal` from a `Traversal` and a list of props */ export const props = ( ...props: [P, P, ...Array

] ): ((sa: Traversal) => Traversal) => compose(pipe(_.lensId(), _.lensProps(...props), _.lensAsTraversal)) /** * Return a `Traversal` from a `Traversal` and a component */ export const component = , P extends keyof A>( prop: P ): ((sa: Traversal) => Traversal) => compose(pipe(_.lensId(), _.lensComponent(prop), _.lensAsTraversal)) /** * Return a `Traversal` from a `Traversal` focused on a `ReadonlyArray` */ export const index = (i: number) => (sa: Traversal>): Traversal => pipe(sa, compose(_.optionalAsTraversal(_.indexArray().index(i)))) /** * Return a `Traversal` from a `Traversal` focused on a `ReadonlyRecord` and a key */ export const keyInRecord = (key: string) => (sa: Traversal>>): Traversal => pipe(sa, compose(_.optionalAsTraversal(_.indexRecord().index(key)))) /** * Return a `Traversal` from a `Traversal` focused on a `ReadonlyRecord` and a required key */ export const atKeyInRecord = (key: string) => (sa: Traversal>>): Traversal> => pipe(sa, compose(_.lensAsTraversal(_.atRecord().at(key)))) /** * Return a `Traversal` from a `Traversal` focused on a `ReadonlyRecord` and a key */ export const keyInHashMap = (key: K) => (sa: Traversal>>): Traversal => pipe(sa, compose(_.optionalAsTraversal(_.indexHashMap().index(key)))) /** * Return a `Traversal` from a `Traversal` focused on a `ReadonlyRecord` and a required key */ export const atKeyInHashMap = (key: K) => (sa: Traversal>>): Traversal> => pipe(sa, compose(_.lensAsTraversal(_.atHashMap().at(key)))) /** * Return a `Traversal` from a `Traversal` focused on the `Some` of a `Option` type */ export const some: (soa: Traversal>) => Traversal = /*#__PURE__*/ compose(_.prismAsTraversal(_.prismSome())) /** * Return a `Traversal` from a `Traversal` focused on the `Right` of a `Either` type */ export const right: (sea: Traversal>) => Traversal = /*#__PURE__*/ compose(_.prismAsTraversal(_.prismRight())) /** * Return a `Traversal` from a `Traversal` focused on the `Left` of a `Either` type */ export const left: (sea: Traversal>) => Traversal = compose(_.prismAsTraversal(_.prismLeft())) /** * Return a `Traversal` from a `Traversal` focused on a `ForEach` */ export function forEach( T: P.ForEach ): ( sta: Traversal> ) => Traversal { return compose(fromForEach(T)()) } /** * Map each target to a `Monoid` and combine the results. */ export const foldMap = (M: Identity) => { const _ = C.getApplicative(M) return (f: (a: A) => M) => (sa: Traversal): ((s: S) => M) => sa.modifyF(_)(f as any) } /** * Map each target to a `Monoid` and combine the results. */ export const fold = (M: Identity): ((sa: Traversal) => (s: S) => A) => foldMap(M)(identity) const unknownId = A.getIdentity() /** * Get all the targets of a `Traversal`. */ export const getAll = (s: S) => (sa: Traversal): ReadonlyArray => foldMap(unknownId)((a: A) => [a])(sa)(s) const unknownIdList = L.getIdentity() /** * Get all the targets of a `Traversal`. */ export const getAllList = (s: S) => (sa: Traversal): L.List => foldMap(unknownIdList as Identity>)(L.of as (a: A) => L.List)(sa)(s) // ------------------------------------------------------------------------------------- // instances // ------------------------------------------------------------------------------------- export const TraversalURI = "monocle/Traversal" export type TraversalURI = typeof TraversalURI declare module "@effect-ts/core/Prelude/HKT" { export interface URItoKind { [TraversalURI]: Traversal } } export const Category = P.instance]>>({ compose, id })