import { Either , chain as eitherChain , isLeft , isRight } from '@typed/either' ; import { List , chain as listChain } from '@typed/list' ; import { Maybe , isJust , isNothing , chain as maybeChain } from '@typed/maybe' ; import { curry2 } from '@typed/functions' ; import { isPromiseLike } from '@typed/logic' ; name export const chain = curry2 < any , any , any > ( function ( f , list ) : any { if ( isJust ( list ) || isNothing ( list ) ) return maybeChain ( f , list ) ; if ( isLeft ( list ) || isRight ( list ) ) return eitherChain ( f , list ) ; if ( isPromiseLike ( list ) ) return Promise . resolve ( list . then ( f ) ) ; return listChain ( f , list ) ; } ) ; export type Chain = { < A , B > ( f , list ) : Array < B > ; < A , B > ( f , maybe ) : Maybe < B > ; < A , B > ( f , promise ) : Promise < B > ; < A , B , C > ( f , either ) : Either < A , C > ; < A , B > ( f ) : ( list : List < A > ) => Array < B > ; < A , B > ( f ) : ( maybe : Maybe < A > ) => Maybe < B > ; < A , B > ( f ) : ( promise : PromiseLike < A > ) => Promise < B > ; < A , B , C > ( f ) : ( either : Either < A , B > ) => Either < A , C > ; } ;