import { ModifyF, Prism, Traversal } from 'monocle-ts' import * as Apl from 'fp-ts/lib/Applicative' import { HKT } from 'fp-ts/lib/HKT' import * as O from 'fp-ts/lib/Option' import * as Th from 'fp-ts/lib/These' export * from 'fp-ts/lib/These' /** * A Prism to select the Left constructor of a These */ export const getLeftPrism = (): Prism, E> => new Prism( Th.fold>( O.some, _a => O.none, (_e, _a) => O.none, ), e => Th.left(e), ) /** * A Prism to select the Right constructor of a These */ export const getRightPrism = (): Prism, A> => new Prism( Th.fold>( _e => O.none, O.some, (_e, _a) => O.none, ), a => Th.right(a), ) /** * A Prism to select the Both constructor of a These */ export const getBothPrism = (): Prism, [E, A]> => new Prism( Th.fold>( _e => O.none, _a => O.none, (e, a) => O.some([e, a]), ), ([e, a]) => Th.both(e, a), ) /** * A ModifyF function for the E value of a These (including Left and Both) * * This allows you to apply an effectful function to modify an E value, whether it's in the Left or Both E slot */ export const getEModifyF = (): ModifyF, E> => ( F_: Apl.Applicative, ) => (f: (e: E) => HKT) => ( these: Th.These, ): HKT> => Th.fold>>( e => F_.map(f(e), Th.left), _a => F_.of(these), (e, a) => F_.map(f(e), e2 => Th.both(e2, a)), )(these) /** * Traversal for the E value of a These (including Left and Both) * * This is an optic for "traversing" the E value of a These, whether it's in the Left or Both slot. */ export const getETraversal = (): Traversal, E> => new Traversal(getEModifyF()) /** * A ModifyF function for the A value of a These (including Right and Both) * * This allows you to apply an effectful function to modify an A value, whether it's in the Right or Both A slot */ export const getAModifyF = (): ModifyF, A> => ( F_: Apl.Applicative, ) => (f: (a: A) => HKT) => ( these: Th.These, ): HKT> => Th.fold>>( _e => F_.of(these), a => F_.map(f(a), Th.right), (e, a) => F_.map(f(a), a2 => Th.both(e, a2)), )(these) /** * Traversal for the A value of a These (including Right and Both) * * This is an optic for "traversing" the A value of a These, whether it's in the Right or Both slot. */ export const getATraversal = (): Traversal, A> => new Traversal(getAModifyF())