since import { Applicative , Applicative1 } from 'fp-ts/lib/Applicative' ; import * as RA from 'fp-ts/lib/ReadonlyArray' ; import * as RNEA from 'fp-ts/lib/ReadonlyNonEmptyArray' ; import * as RR from 'fp-ts/lib/ReadonlyRecord' ; import { constant , flow , identity , Predicate } from 'fp-ts/lib/function' ; import { HKT , Kind , Kind2 , Kind3 , URIS , URIS2 , URIS3 } from 'fp-ts/lib/HKT' ; import * as O from 'fp-ts/lib/Option' ; import * as E from 'fp-ts/lib/Either' ; import { pipe } from 'fp-ts/lib/pipeable' ; import { Traversable , Traversable1 , Traversable2 , Traversable3 } from 'fp-ts/lib/Traversable' ; import { Iso } from './Iso' ; import { Index } from './Ix' ; import { Lens } from './Lens' ; import { Optional } from './Optional' ; import { Prism } from './Prism' ; import { Traversal } from './Traversal' ; import { At } from './At' ; import { NonEmptyArray } from 'fp-ts/lib/NonEmptyArray' ; import { URI as IURI } from 'fp-ts/lib/Identity' ; internal export const iso = < S , A > ( get , reverseGet ) : Iso < S , A > => ( { get , reverseGet } ) ; internal export const isoAsLens = < S , A > ( sa ) : Lens < S , A > => lens ( sa . get , flow ( sa . reverseGet , constant ) ) ; internal export const isoAsPrism = < S , A > ( sa ) : Prism < S , A > => prism ( flow ( sa . get , O . some ) , sa . reverseGet ) ; internal export const isoAsOptional = < S , A > ( sa ) : Optional < S , A > => optional ( flow ( sa . get , O . some ) , flow ( sa . reverseGet , constant ) ) ; internal export const isoAsTraversal = < S , A > ( sa ) : Traversal < S , A > => traversal ( < F > ( F ) => ( f ) => ( s ) => F . map ( f ( sa . get ( s ) ) , ( a ) => sa . reverseGet ( a ) ) ) ; internal export const lens = < S , A > ( get , set ) : Lens < S , A > => ( { get , set } ) ; internal export const lensAsOptional = < S , A > ( sa ) : Optional < S , A > => optional ( flow ( sa . get , O . some ) , sa . set ) ; internal export const lensAsTraversal = < S , A > ( sa ) : Traversal < S , A > => traversal ( < F > ( F ) => ( f ) => ( s ) => F . map ( f ( sa . get ( s ) ) , ( a ) => sa . set ( a ) ( s ) ) ) ; internal export const lensComposeLens = < A , B > ( ab ) => < S > ( sa ) : Lens < S , B > => lens ( ( s ) => ab . get ( sa . get ( s ) ) , ( b ) => ( s ) => sa . set ( ab . set ( b ) ( sa . get ( s ) ) ) ( s ) ) ; internal export const prismComposePrism = < A , B > ( ab ) => < S > ( sa ) : Prism < S , B > => prism ( flow ( sa . getOption , O . chain ( ab . getOption ) ) , flow ( ab . reverseGet , sa . reverseGet ) ) ; internal export const lensComposePrism = < A , B > ( ab ) => < S > ( sa ) : Optional < S , B > => optionalComposeOptional ( prismAsOptional ( ab ) ) ( lensAsOptional ( sa ) ) ; internal export const lensId = < S > ( ) : Lens < S , S > => lens ( identity , constant ) ; internal export const lensProp = < A , P extends keyof A > ( prop ) => < S > ( sa ) : Lens < S , A [ P ] > => lens ( ( s ) => sa . get ( s ) [ prop ] , ( ap ) => ( s ) => { const oa = sa . get ( s ) ; if ( ap === oa [ prop ] ) { return s ; } return sa . set ( Object . assign ( { } , oa , { [ prop ] : ap } ) ) ( s ) ; } ) ; internal export const lensProps = < A , P extends keyof A > ( ... props ) => < S > ( sa ) : Lens < S , { [ K in P ] : A [ K ] ; } > => lens ( ( s ) => { const a = sa . get ( s ) ; const r = { } ; for ( const k of props ) { r [ k ] = a [ k ] ; } return r as any ; } , ( a ) => ( s ) => { const oa = sa . get ( s ) ; for ( const k of props ) { if ( a [ k ] !== oa [ k ] ) { return sa . set ( Object . assign ( { } , oa , a ) ) ( s ) ; } } return s ; } ) ; internal export const lensComponent = < A extends ReadonlyArray < unknown > , P extends keyof A > ( prop ) => < S > ( sa ) : Lens < S , A [ P ] > => lens ( ( s ) => sa . get ( s ) [ prop ] , ( ap ) => ( s ) => { const oa = sa . get ( s ) ; if ( ap === oa [ prop ] ) { return s ; } const copy = oa . slice ( ) as any ; copy [ prop ] = ap ; return sa . set ( copy ) ( s ) ; } ) ; internal export const lensAtKey = ( key ) => < S , A > ( sa ) : Lens < S , O . Option < A > > => pipe ( sa , lensComposeLens ( atReadonlyRecord < A > ( ) . at ( key ) ) ) ; internal export const prism = < S , A > ( getOption , reverseGet ) : Prism < S , A > => ( { getOption , reverseGet } ) ; internal export const prismAsOptional = < S , A > ( sa ) : Optional < S , A > => optional ( sa . getOption , ( a ) => prismSet ( a ) ( sa ) ) ; internal export const prismAsTraversal = < S , A > ( sa ) : Traversal < S , A > => traversal ( < F > ( F ) => ( f ) => ( s ) => pipe ( sa . getOption ( s ) , O . fold ( ( ) => F . of ( s ) , ( a ) => F . map ( f ( a ) , ( a ) => prismSet ( a ) ( sa ) ( s ) ) ) ) ) ; internal export const prismModifyOption = < A > ( f ) => < S > ( sa ) => ( s ) : O . Option < S > => pipe ( sa . getOption ( s ) , O . map ( ( o ) => { const n = f ( o ) ; return n === o ? s : sa . reverseGet ( n ) ; } ) ) ; internal export const prismModify = < A > ( f ) => < S > ( sa ) : ( ( s : S ) => S ) => { const g = prismModifyOption ( f ) ( sa ) ; return ( s ) => pipe ( g ( s ) , O . getOrElse ( ( ) => s ) ) ; } ; internal export const prismSet = < A > ( a ) : ( < S > ( sa : Prism < S , A > ) => ( s : S ) => S ) => prismModify ( ( ) => a ) ; internal export const prismComposeLens = < A , B > ( ab ) => < S > ( sa ) : Optional < S , B > => optionalComposeOptional ( lensAsOptional ( ab ) ) ( prismAsOptional ( sa ) ) ; internal export const prismFromNullable = < A > ( ) : Prism < A , NonNullable < A > > => prism ( O . fromNullable , identity ) ; internal export const prismFromPredicate = < A > ( predicate ) : Prism < A , A > => prism ( O . fromPredicate ( predicate ) , identity ) ; internal export const prismSome = < A > ( ) : Prism < O . Option < A > , A > => prism ( identity , O . some ) ; internal export const prismRight = < E , A > ( ) : Prism < E . Either < E , A > , A > => prism ( O . fromEither , E . right ) ; internal export const prismLeft = < E , A > ( ) : Prism < E . Either < E , A > , E > => prism ( ( s ) => ( E . isLeft ( s ) ? O . some ( s . left ) : O . none ) , E . left ) ; internal export const optional = < S , A > ( getOption , set ) : Optional < S , A > => ( { getOption , set } ) ; internal export const optionalAsTraversal = < S , A > ( sa ) : Traversal < S , A > => traversal ( < F > ( F ) => ( f ) => ( s ) => pipe ( sa . getOption ( s ) , O . fold ( ( ) => F . of ( s ) , ( a ) => F . map ( f ( a ) , ( a ) => sa . set ( a ) ( s ) ) ) ) ) ; internal export const optionalModifyOption = < A > ( f ) => < S > ( optional ) => ( s ) : O . Option < S > => pipe ( optional . getOption ( s ) , O . map ( ( a ) => { const n = f ( a ) ; return n === a ? s : optional . set ( n ) ( s ) ; } ) ) ; internal export const optionalModify = < A > ( f ) => < S > ( optional ) : ( ( s : S ) => S ) => { const g = optionalModifyOption ( f ) ( optional ) ; return ( s ) => pipe ( g ( s ) , O . getOrElse ( ( ) => s ) ) ; } ; internal export const optionalComposeOptional = < A , B > ( ab ) => < S > ( sa ) : Optional < S , B > => optional ( flow ( sa . getOption , O . chain ( ab . getOption ) ) , ( b ) => optionalModify ( ab . set ( b ) ) ( sa ) ) ; internal export const optionalIndex = ( i ) => < S , A > ( sa ) : Optional < S , A > => pipe ( sa , optionalComposeOptional ( indexReadonlyArray < A > ( ) . index ( i ) ) ) ; internal export const optionalIndexNonEmpty = ( i ) => < S , A > ( sa ) : Optional < S , A > => pipe ( sa , optionalComposeOptional ( indexReadonlyNonEmptyArray < A > ( ) . index ( i ) ) ) ; internal export const optionalKey = ( key ) => < S , A > ( sa ) : Optional < S , A > => pipe ( sa , optionalComposeOptional ( indexReadonlyRecord < A > ( ) . index ( key ) ) ) ; internal export const optionalFindFirst = < A > ( predicate ) : Optional < ReadonlyArray < A > , A > => optional ( RA . findFirst ( predicate ) , ( a ) => ( s ) => pipe ( RA . findIndex ( predicate ) ( s ) , O . fold ( ( ) => s , ( i ) => RA . unsafeUpdateAt ( i , a , s ) ) ) ) ; const unsafeUpdateAt = < A > ( i , a , as ) : RNEA . ReadonlyNonEmptyArray < A > => { if ( as [ i ] === a ) { return as ; } else { const xs = [ as [ 0 ] , ... as . slice ( 1 ) ] ; xs [ i ] = a ; return xs ; } } ; internal export const optionalFindFirstNonEmpty = < A > ( predicate ) : Optional < RNEA . ReadonlyNonEmptyArray < A > , A > => optional < RNEA . ReadonlyNonEmptyArray < A > , A > ( RA . findFirst ( predicate ) , ( a ) => ( as ) => pipe ( RA . findIndex ( predicate ) ( as ) , O . fold ( ( ) => as , ( i ) => unsafeUpdateAt ( i , a , as ) ) ) ) ; internal export const traversal = < S , A > ( modifyF ) : Traversal < S , A > => ( { modifyF } ) ; internal export function traversalComposeTraversal < A , B > ( ab ) { return ( sa ) => traversal ( < F > ( F ) => ( f ) => sa . modifyF ( F ) ( ab . modifyF ( F ) ( f ) ) ) ; } internal export const ApplicativeIdentity = { URI : 'Identity' , map : ( fa , f ) => f ( fa ) , of : identity , ap : ( fab , fa ) => fab ( fa ) } ; const isIdentity = ( F ) : boolean => F . URI === 'Identity' ; internal export function fromTraversable < T extends URIS3 > ( T ) ; internal export function fromTraversable < T extends URIS2 > ( T ) ; internal export function fromTraversable < T extends URIS > ( T ) ; internal export function fromTraversable < T > ( T ) ; export function fromTraversable < T > ( T ) { return < A > ( ) => traversal ( < F > ( F ) => { const traverseF = isIdentity ( F ) ? ( T . map as any ) : T . traverse ( F ) ; return ( f ) => ( s ) => traverseF ( s , f ) ; } ) ; } internal export function traversalTraverse < T extends URIS > ( T ) { return traversalComposeTraversal ( fromTraversable ( T ) ( ) ) ; } internal export const index = < S , I , A > ( index ) : Index < S , I , A > => ( { index } ) ; internal export const indexReadonlyArray = < A = never > ( ) : Index < ReadonlyArray < A > , number , A > => index ( ( i ) => optional ( ( as ) => RA . lookup ( i , as ) , ( a ) => ( as ) => pipe ( RA . lookup ( i , as ) , O . fold ( ( ) => as , ( ) => RA . unsafeUpdateAt ( i , a , as ) ) ) ) ) ; internal export const indexReadonlyNonEmptyArray = < A = never > ( ) : Index < RNEA . ReadonlyNonEmptyArray < A > , number , A > => index ( ( i ) => optional ( ( as ) => RA . lookup ( i , as ) , ( a ) => ( as ) => pipe ( RA . lookup ( i , as ) , O . fold ( ( ) => as , ( ) => unsafeUpdateAt ( i , a , as ) ) ) ) ) ; internal export const indexReadonlyRecord = < A = never > ( ) : Index < RR . ReadonlyRecord < string , A > , string , A > => index ( ( k ) => optional ( ( r ) => RR . lookup ( k , r ) , ( a ) => ( r ) => { if ( r [ k ] === a || O . isNone ( RR . lookup ( k , r ) ) ) { return r ; } return RR . insertAt ( k , a ) ( r ) ; } ) ) ; internal export const at = < S , I , A > ( at ) : At < S , I , A > => ( { at } ) ; internal export function atReadonlyRecord < A = never > ( ) { return at ( ( key ) => lens ( ( r ) => RR . lookup ( key , r ) , O . fold ( ( ) => RR . deleteAt ( key ) , ( a ) => RR . insertAt ( key , a ) ) ) ) ; }