since import * as A from 'fp-ts/lib/Array' ; import { getApplicative , make } from 'fp-ts/lib/Const' ; import { Foldable , Foldable1 , Foldable2 , Foldable3 } from 'fp-ts/lib/Foldable' ; import { constant , identity , Predicate , Refinement } from 'fp-ts/lib/function' ; import { HKT , Kind , Kind2 , Kind3 , URIS , URIS2 , URIS3 } from 'fp-ts/lib/HKT' ; import { Monoid , monoidAll , monoidAny } from 'fp-ts/lib/Monoid' ; import { fromNullable , fromPredicate , getFirstMonoid , isNone , Option , option , some } from 'fp-ts/lib/Option' ; import { pipe } from 'fp-ts/lib/pipeable' ; import { Traversable , Traversable1 , Traversable2 , Traversable3 } from 'fp-ts/lib/Traversable' ; import * as at from './At' ; import * as iso from './Iso' ; import * as index from './Ix' ; import * as lens from './Lens' ; import * as optional from './Optional' ; import * as prism from './Prism' ; import * as traversal from './Traversal' ; export { at , iso , index , lens , prism , optional , traversal } ; const fromIso = < S , A > ( iso ) : Iso < S , A > => new Iso ( iso . get , iso . reverseGet ) ; const fromLens = < S , A > ( lens ) : Lens < S , A > => new Lens ( lens . get , lens . set ) ; const fromPrism = < S , A > ( prism ) : Prism < S , A > => new Prism ( prism . getOption , prism . reverseGet ) ; const fromOptional = < S , A > ( optional ) : Optional < S , A > => new Optional ( optional . getOption , optional . set ) ; const fromTraversal = < S , A > ( traversal ) : Traversal < S , A > => new Traversal ( traversal . modifyF ) ; const fromAt = < S , I , A > ( at ) : At < S , I , A > => new At ( ( i ) => fromLens ( at . at ( i ) ) ) ; const fromIndex = < S , I , A > ( ix ) : Index < S , I , A > => new Index ( ( i ) => fromOptional ( ix . index ( i ) ) ) ; const update = < O , K extends keyof O , A extends O [ K ] > ( o , k , a ) : O => { return a === o [ k ] ? o : Object . assign ( { } , o , { [ k ] : a } ) ; } ; category since export class Iso < S , A > { since readonly _tag : 'Iso' = 'Iso' ; since readonly unwrap = this . get ; since readonly to = this . get ; since readonly wrap = this . reverseGet ; since readonly from = this . reverseGet ; constructor ( readonly get , readonly reverseGet ) { } since reverse ( ) { return fromIso ( iso . reverse ( this ) ) ; } since modify ( f ) { return iso . modify ( f ) ( this ) ; } since asLens ( ) { return fromLens ( iso . asLens ( this ) ) ; } since asPrism ( ) { return fromPrism ( iso . asPrism ( this ) ) ; } since asOptional ( ) { return fromOptional ( iso . asOptional ( this ) ) ; } since asTraversal ( ) { return fromTraversal ( iso . asTraversal ( this ) ) ; } since asFold ( ) { return new Fold ( ( ) => ( f ) => ( s ) => f ( this . get ( s ) ) ) ; } since asGetter ( ) { return new Getter ( ( s ) => this . get ( s ) ) ; } since asSetter ( ) { return new Setter ( ( f ) => this . modify ( f ) ) ; } since compose < B > ( ab ) { return fromIso ( iso . compose ( ab ) ( this ) ) ; } since composeIso < B > ( ab ) { return this . compose ( ab ) ; } since composeLens < B > ( ab ) { return fromLens ( pipe ( this , iso . asLens , lens . compose ( ab ) ) ) ; } since composePrism < B > ( ab ) { return fromPrism ( pipe ( this , iso . asPrism , prism . compose ( ab ) ) ) ; } since composeOptional < B > ( ab ) { return fromOptional ( pipe ( this , iso . asOptional , optional . compose ( ab ) ) ) ; } since composeTraversal < B > ( ab ) { return fromTraversal ( pipe ( this , iso . asTraversal , traversal . compose ( ab ) ) ) ; } since composeFold < B > ( ab ) { return this . asFold ( ) . compose ( ab ) ; } since composeGetter < B > ( ab ) { return this . asGetter ( ) . compose ( ab ) ; } since composeSetter < B > ( ab ) { return this . asSetter ( ) . compose ( ab ) ; } } since export interface LensFromPath < S > { < K1 extends keyof S , K2 extends keyof S [ K1 ] , K3 extends keyof S [ K1 ] [ K2 ] , K4 extends keyof S [ K1 ] [ K2 ] [ K3 ] , K5 extends keyof S [ K1 ] [ K2 ] [ K3 ] [ K4 ] > ( path ) : Lens < S , S [ K1 ] [ K2 ] [ K3 ] [ K4 ] [ K5 ] > ; < K1 extends keyof S , K2 extends keyof S [ K1 ] , K3 extends keyof S [ K1 ] [ K2 ] , K4 extends keyof S [ K1 ] [ K2 ] [ K3 ] > ( path ) : Lens < S , S [ K1 ] [ K2 ] [ K3 ] [ K4 ] > ; < K1 extends keyof S , K2 extends keyof S [ K1 ] , K3 extends keyof S [ K1 ] [ K2 ] > ( path ) : Lens < S , S [ K1 ] [ K2 ] [ K3 ] > ; < K1 extends keyof S , K2 extends keyof S [ K1 ] > ( path ) : Lens < S , S [ K1 ] [ K2 ] > ; < K1 extends keyof S > ( path ) : Lens < S , S [ K1 ] > ; } category since export class Lens < S , A > { since readonly _tag : 'Lens' = 'Lens' ; constructor ( readonly get , readonly set ) { } example since static fromPath < S > ( ) { const fromProp = Lens . fromProp < S > ( ) ; return ( path ) => { const lens = fromProp ( path [ 0 ] ) ; return path . slice ( 1 ) . reduce ( ( acc , prop ) => acc . compose ( fromProp ( prop ) ) , lens ) ; } ; } example since static fromProp < S > ( ) { return ( prop ) => fromLens ( pipe ( lens . id < S > ( ) , lens . prop ( prop ) ) ) ; } example since static fromProps < S > ( ) ; static fromProps < S > ( ) { return ( props ) => fromLens ( pipe ( lens . id < S > ( ) , lens . props ( ... props ) ) ) ; } example since static fromNullableProp < S > ( ) { return ( k , defaultValue ) => new Lens ( ( s ) => { const osk = fromNullable ( s [ k ] ) ; if ( isNone ( osk ) ) { return defaultValue ; } else { return osk . value as any ; } } , ( a ) => ( s ) => update ( s , k , a ) ) ; } since modify ( f ) { return lens . modify ( f ) ( this ) ; } since asOptional ( ) { return fromOptional ( lens . asOptional ( this ) ) ; } since asTraversal ( ) { return fromTraversal ( lens . asTraversal ( this ) ) ; } since asSetter ( ) { return new Setter ( ( f ) => this . modify ( f ) ) ; } since asGetter ( ) { return new Getter ( ( s ) => this . get ( s ) ) ; } since asFold ( ) { return new Fold ( ( ) => ( f ) => ( s ) => f ( this . get ( s ) ) ) ; } since compose < B > ( ab ) { return fromLens ( lens . compose ( ab ) ( this ) ) ; } since composeLens < B > ( ab ) { return this . compose ( ab ) ; } since composeGetter < B > ( ab ) { return this . asGetter ( ) . compose ( ab ) ; } since composeFold < B > ( ab ) { return this . asFold ( ) . compose ( ab ) ; } since composeOptional < B > ( ab ) { return fromOptional ( pipe ( this , lens . asOptional , optional . compose ( ab ) ) ) ; } since composeTraversal < B > ( ab ) { return fromTraversal ( pipe ( this , lens . asTraversal , traversal . compose ( ab ) ) ) ; } since composeSetter < B > ( ab ) { return this . asSetter ( ) . compose ( ab ) ; } since composeIso < B > ( ab ) { return fromLens ( pipe ( this , lens . compose ( pipe ( ab , iso . asLens ) ) ) ) ; } since composePrism < B > ( ab ) { return fromOptional ( lens . composePrism ( ab ) ( this ) ) ; } } category since export class Prism < S , A > { since readonly _tag : 'Prism' = 'Prism' ; constructor ( readonly getOption , readonly reverseGet ) { } since static fromPredicate < S , A extends S > ( refinement ) ; static fromPredicate < A > ( predicate ) ; static fromPredicate < A > ( predicate ) { return fromPrism ( prism . fromPredicate ( predicate ) ) ; } since static some < A > ( ) { return somePrism ; } since modify ( f ) { return ( s ) => { const os = this . modifyOption ( f ) ( s ) ; if ( isNone ( os ) ) { return s ; } else { return os . value ; } } ; } since modifyOption ( f ) { return ( s ) => option . map ( this . getOption ( s ) , ( v ) => { const n = f ( v ) ; return n === v ? s : this . reverseGet ( n ) ; } ) ; } since set ( a ) { return this . modify ( ( ) => a ) ; } since asOptional ( ) { return fromOptional ( prism . asOptional ( this ) ) ; } since asTraversal ( ) { return fromTraversal ( prism . asTraversal ( this ) ) ; } since asSetter ( ) { return new Setter ( ( f ) => this . modify ( f ) ) ; } since asFold ( ) { return new Fold ( < M > ( M ) => ( f ) => ( s ) : M => { const oa = this . getOption ( s ) ; return isNone ( oa ) ? M . empty : f ( oa . value ) ; } ) ; } since compose < B > ( ab ) { return fromPrism ( prism . compose ( ab ) ( this ) ) ; } since composePrism < B > ( ab ) { return this . compose ( ab ) ; } since composeOptional < B > ( ab ) { return fromOptional ( pipe ( this , prism . asOptional , optional . compose ( ab ) ) ) ; } since composeTraversal < B > ( ab ) { return fromTraversal ( pipe ( this , prism . asTraversal , traversal . compose ( ab ) ) ) ; } since composeFold < B > ( ab ) { return this . asFold ( ) . compose ( ab ) ; } since composeSetter < B > ( ab ) { return this . asSetter ( ) . compose ( ab ) ; } since composeIso < B > ( ab ) { return fromPrism ( pipe ( this , prism . compose ( pipe ( ab , iso . asPrism ) ) ) ) ; } since composeLens < B > ( ab ) { return fromOptional ( prism . composeLens ( ab ) ( this ) ) ; } since composeGetter < B > ( ab ) { return this . asFold ( ) . compose ( ab . asFold ( ) ) ; } } const somePrism = new Prism < Option < any > , any > ( identity , some ) ; type OptionPropertyNames < S > = { [ K in keyof S ] - ? : S [ K ] extends Option < any > ? K : never ; } [ keyof S ] ; type OptionPropertyType < S , K extends OptionPropertyNames < S > > = S [ K ] extends Option < infer A > ? A : never ; since export interface OptionalFromPath < S > { < K1 extends keyof S , K2 extends keyof NonNullable < S [ K1 ] > , K3 extends keyof NonNullable < NonNullable < S [ K1 ] > [ K2 ] > , K4 extends keyof NonNullable < NonNullable < NonNullable < S [ K1 ] > [ K2 ] > [ K3 ] > , K5 extends keyof NonNullable < NonNullable < NonNullable < NonNullable < S [ K1 ] > [ K2 ] > [ K3 ] > [ K4 ] > > ( path ) : Optional < S , NonNullable < NonNullable < NonNullable < NonNullable < NonNullable < S [ K1 ] > [ K2 ] > [ K3 ] > [ K4 ] > [ K5 ] > > ; < K1 extends keyof S , K2 extends keyof NonNullable < S [ K1 ] > , K3 extends keyof NonNullable < NonNullable < S [ K1 ] > [ K2 ] > , K4 extends keyof NonNullable < NonNullable < NonNullable < S [ K1 ] > [ K2 ] > [ K3 ] > > ( path ) : Optional < S , NonNullable < NonNullable < NonNullable < NonNullable < S [ K1 ] > [ K2 ] > [ K3 ] > [ K4 ] > > ; < K1 extends keyof S , K2 extends keyof NonNullable < S [ K1 ] > , K3 extends keyof NonNullable < NonNullable < S [ K1 ] > [ K2 ] > > ( path ) : Optional < S , NonNullable < NonNullable < NonNullable < S [ K1 ] > [ K2 ] > [ K3 ] > > ; < K1 extends keyof S , K2 extends keyof NonNullable < S [ K1 ] > > ( path ) : Optional < S , NonNullable < NonNullable < S [ K1 ] > [ K2 ] > > ; < K1 extends keyof S > ( path ) : Optional < S , NonNullable < S [ K1 ] > > ; } category since export class Optional < S , A > { since readonly _tag : 'Optional' = 'Optional' ; constructor ( readonly getOption , readonly set ) { } example since static fromPath < S > ( ) { const fromNullableProp = Optional . fromNullableProp < S > ( ) ; return ( path ) => { const optional = fromNullableProp ( path [ 0 ] ) ; return path . slice ( 1 ) . reduce ( ( acc , prop ) => acc . compose ( fromNullableProp ( prop ) ) , optional ) ; } ; } example since static fromNullableProp < S > ( ) { return ( k ) => new Optional ( ( s ) => fromNullable ( s [ k ] ) , ( a ) => ( s ) => ( s [ k ] == null ? s : update ( s , k , a ) ) ) ; } example since static fromOptionProp < S > ( ) { const formProp = Lens . fromProp < S > ( ) ; return ( prop ) => formProp ( prop ) . composePrism ( somePrism as any ) ; } since modify ( f ) { return optional . modify ( f ) ( this ) ; } since modifyOption ( f ) { return optional . modifyOption ( f ) ( this ) ; } since asTraversal ( ) { return fromTraversal ( optional . asTraversal ( this ) ) ; } since asFold ( ) { return new Fold ( < M > ( M ) => ( f ) => ( s ) : M => { const oa = this . getOption ( s ) ; return isNone ( oa ) ? M . empty : f ( oa . value ) ; } ) ; } since asSetter ( ) { return new Setter ( ( f ) => this . modify ( f ) ) ; } since compose < B > ( ab ) { return fromOptional ( optional . compose ( ab ) ( this ) ) ; } since composeOptional < B > ( ab ) { return this . compose ( ab ) ; } since composeTraversal < B > ( ab ) { return fromTraversal ( pipe ( this , optional . asTraversal , traversal . compose ( ab ) ) ) ; } since composeFold < B > ( ab ) { return this . asFold ( ) . compose ( ab ) ; } since composeSetter < B > ( ab ) { return this . asSetter ( ) . compose ( ab ) ; } since composeLens < B > ( ab ) { return fromOptional ( pipe ( this , optional . compose ( pipe ( ab , lens . asOptional ) ) ) ) ; } since composePrism < B > ( ab ) { return fromOptional ( pipe ( this , optional . compose ( pipe ( ab , prism . asOptional ) ) ) ) ; } since composeIso < B > ( ab ) { return fromOptional ( pipe ( this , optional . compose ( pipe ( ab , iso . asOptional ) ) ) ) ; } since composeGetter < B > ( ab ) { return this . asFold ( ) . compose ( ab . asFold ( ) ) ; } } since export type ModifyF < S , A > = traversal . ModifyF < S , A > ; category since export class Traversal < S , A > { since readonly _tag : 'Traversal' = 'Traversal' ; constructor ( readonly modifyF ) { } since modify ( f ) { return traversal . modify ( f ) ( this ) ; } since set ( a ) { return traversal . set ( a ) ( this ) ; } example since filter < B extends A > ( refinement ) ; filter ( predicate ) ; filter ( predicate ) { return fromTraversal ( traversal . filter ( predicate ) ( this ) ) ; } since asFold ( ) { return new Fold ( < M > ( M ) => ( f ) : ( ( s : S ) => M ) => this . modifyF ( getApplicative ( M ) ) ( ( a ) => make ( f ( a ) ) ) ) ; } since asSetter ( ) { return new Setter ( ( f ) => this . modify ( f ) ) ; } since compose < B > ( ab ) { return fromTraversal ( traversal . compose ( ab ) ( this ) ) ; } since composeTraversal < B > ( ab ) { return this . compose ( ab ) ; } since composeFold < B > ( ab ) { return this . asFold ( ) . compose ( ab ) ; } since composeSetter < B > ( ab ) { return this . asSetter ( ) . compose ( ab ) ; } since composeOptional < B > ( ab ) { return this . compose ( ab . asTraversal ( ) ) ; } since composeLens < B > ( ab ) { return fromTraversal ( pipe ( this , traversal . compose ( pipe ( ab , lens . asTraversal ) ) ) ) ; } since composePrism < B > ( ab ) { return fromTraversal ( pipe ( this , traversal . compose ( pipe ( ab , prism . asTraversal ) ) ) ) ; } since composeIso < B > ( ab ) { return fromTraversal ( pipe ( this , traversal . compose ( pipe ( ab , iso . asTraversal ) ) ) ) ; } since composeGetter < B > ( ab ) { return this . asFold ( ) . compose ( ab . asFold ( ) ) ; } } category since export class At < S , I , A > { since readonly _tag : 'At' = 'At' ; constructor ( readonly at ) { } since fromIso < T > ( iso ) { return fromAt ( at . fromIso ( iso ) ( this ) ) ; } } category since export class Index < S , I , A > { since readonly _tag : 'Index' = 'Index' ; constructor ( readonly index ) { } since static fromAt < T , J , B > ( at ) { return fromIndex ( index . fromAt ( at ) ) ; } since fromIso < T > ( iso ) { return fromIndex ( index . fromIso ( iso ) ( this ) ) ; } } category since export class Getter < S , A > { since readonly _tag : 'Getter' = 'Getter' ; constructor ( readonly get ) { } since asFold ( ) { return new Fold ( ( ) => ( f ) => ( s ) => f ( this . get ( s ) ) ) ; } since compose < B > ( ab ) { return new Getter ( ( s ) => ab . get ( this . get ( s ) ) ) ; } since composeGetter < B > ( ab ) { return this . compose ( ab ) ; } since composeFold < B > ( ab ) { return this . asFold ( ) . compose ( ab ) ; } since composeLens < B > ( ab ) { return this . compose ( ab . asGetter ( ) ) ; } since composeIso < B > ( ab ) { return this . compose ( ab . asGetter ( ) ) ; } since composeTraversal < B > ( ab ) { return this . asFold ( ) . compose ( ab . asFold ( ) ) ; } since composeOptional < B > ( ab ) { return this . asFold ( ) . compose ( ab . asFold ( ) ) ; } since composePrism < B > ( ab ) { return this . asFold ( ) . compose ( ab . asFold ( ) ) ; } } category since export class Fold < S , A > { since readonly _tag : 'Fold' = 'Fold' ; since readonly getAll : ( s : S ) => Array < A > ; since readonly exist : ( p : Predicate < A > ) => Predicate < S > ; since readonly all : ( p : Predicate < A > ) => Predicate < S > ; private readonly foldMapFirst : ( f : ( a : A ) => Option < A > ) => ( s : S ) => Option < A > ; constructor ( readonly foldMap ) { this . getAll = foldMap ( A . getMonoid < A > ( ) ) ( A . of ) ; this . exist = foldMap ( monoidAny ) ; this . all = foldMap ( monoidAll ) ; this . foldMapFirst = foldMap ( getFirstMonoid ( ) ) ; } since compose < B > ( ab ) { return new Fold ( < M > ( M ) => ( f ) => this . foldMap ( M ) ( ab . foldMap ( M ) ( f ) ) ) ; } since composeFold < B > ( ab ) { return this . compose ( ab ) ; } since composeGetter < B > ( ab ) { return this . compose ( ab . asFold ( ) ) ; } since composeTraversal < B > ( ab ) { return this . compose ( ab . asFold ( ) ) ; } since composeOptional < B > ( ab ) { return this . compose ( ab . asFold ( ) ) ; } since composeLens < B > ( ab ) { return this . compose ( ab . asFold ( ) ) ; } since composePrism < B > ( ab ) { return this . compose ( ab . asFold ( ) ) ; } since composeIso < B > ( ab ) { return this . compose ( ab . asFold ( ) ) ; } since find < B extends A > ( p ) ; find ( p ) ; find ( p ) { return this . foldMapFirst ( fromPredicate ( p ) ) ; } since headOption ( s ) { return this . find ( ( ) => true ) ( s ) ; } } category since export class Setter < S , A > { since readonly _tag : 'Setter' = 'Setter' ; constructor ( readonly modify ) { } since set ( a ) { return this . modify ( constant ( a ) ) ; } since compose < B > ( ab ) { return new Setter ( ( f ) => this . modify ( ab . modify ( f ) ) ) ; } since composeSetter < B > ( ab ) { return this . compose ( ab ) ; } since composeTraversal < B > ( ab ) { return this . compose ( ab . asSetter ( ) ) ; } since composeOptional < B > ( ab ) { return this . compose ( ab . asSetter ( ) ) ; } since composeLens < B > ( ab ) { return this . compose ( ab . asSetter ( ) ) ; } since composePrism < B > ( ab ) { return this . compose ( ab . asSetter ( ) ) ; } since composeIso < B > ( ab ) { return this . compose ( ab . asSetter ( ) ) ; } } example category since export function fromTraversable < T extends URIS3 > ( T ) ; export function fromTraversable < T extends URIS2 > ( T ) ; export function fromTraversable < T extends URIS > ( T ) ; export function fromTraversable < T > ( T ) ; export function fromTraversable < T > ( T ) { const f = traversal . fromTraversable ( T ) ; return < A > ( ) => fromTraversal ( f < A > ( ) ) ; } category since export function fromFoldable < F extends URIS3 > ( F ) ; export function fromFoldable < F extends URIS2 > ( F ) ; export function fromFoldable < F extends URIS > ( F ) ; export function fromFoldable < F > ( F ) ; export function fromFoldable < F > ( F ) { return < A > ( ) => new Fold < HKT < F , A > , A > ( < M > ( M ) => { const foldMapFM = F . foldMap ( M ) ; return ( f ) => ( s ) => foldMapFM ( s , f ) ; } ) ; }