import { test } from "../../tests/harness" ; import { GenericMonad } from "./adt" ; const array = ( ( ) => { const nil = [ ] as never [ ] ; const cons = < A > ( head ) => ( rest ) : A [ ] => [ head , ... rest ] ; const match = < A > ( { cons , nil } ) => ( a ) => ( a . length === 0 ? nil : cons ( a [ 0 ] ) ( a . slice ( 1 ) ) ) ; return { nil , cons , match } ; } ) ( ) ; const GenericList = < G extends GenericMonad < any > > ( { cons , nil , match } ) => { const length = match ( { nil : 0 , cons : _ => rest => 1 + length ( rest ) } ) ; const map = f => match ( { nil , cons : head => rest => cons ( f ( head ) ) ( map ( f ) ( rest ) ) } ) ; return { length , map } ; } ; test ( "examples/pattern-matching/list" , test => { test . plan ( 2 ) ; test . equal < number > ( 3 ) ( GenericList < any [ ] > ( array ) . length ( [ 0 , 1 , 2 ] ) ) ; test . deepEqual < number [ ] > ( [ 2 , 4 , 6 ] ) ( GenericList < any [ ] > ( array ) . map ( ( x ) => x * 2 ) ( [ 1 , 2 , 3 ] ) ) ; } ) ;