@@ -25,19 +25,21 @@ const {"insert-by":insertBy,"sort-by":sortBy,reverse} = solution;
2525const { "zip-with" :zipWith , zip, unzip} = solution ;
2626const { "group-by" :groupBy , "nub-by" :nubBy , "delete-by" :deleteBy , "delete-firsts-by" :deleteFirstsBy } = solution ;
2727const { init, last, tails, inits, slice, transpose} = solution ;
28- const { add, zero} = solution ;
28+ const { zero , succ , pred , add, "is- zero" : isZero , Pair , None , Some } = solution ;
2929
3030const fromInt = LC . fromIntWith ( LC . config ) ;
3131const toInt = LC . toIntWith ( LC . config ) ;
3232const fromArray = xs => xs . reduceRight ( ( z , x ) => cons ( x ) ( z ) , nil ) ;
3333const toArray = foldl ( z => x => [ ...z , x ] ) ( [ ] ) ;
3434const fromPair = ( [ fst , snd ] ) => Pair ( fst ) ( snd ) ;
3535const toPair = xy => xy ( fst => snd => [ fst , snd ] ) ;
36+ const fromNullable = x => x === null ? None : Some ( x ) ;
3637const toNullable = fn => optX => optX ( null ) ( fn ) ;
3738
3839const rnd = ( m , n = 0 ) => Math . random ( ) * ( n - m ) + m | 0 ;
3940const elements = xs => xs [ rnd ( xs . length ) ] ;
4041const rndArray = size => Array . from ( { length : rnd ( size ) } , ( ) => rnd ( size ) ) ;
42+ const rndNonEmptyArray = size => Array . from ( { length : rnd ( size ) || 1 } , ( ) => rnd ( size ) ) ;
4143
4244describe ( "Scott Lists" , function ( ) {
4345 it ( "nil,cons,singleton" , ( ) => {
@@ -80,4 +82,47 @@ describe("Scott Lists",function(){
8082 `after ${ i } tests` ) ;
8183 }
8284 } ) ;
85+ it ( "iterate" , ( ) => {
86+ const N = 10 ;
87+ for ( let i = 1 ; i <= 10 ; i ++ ) {
88+ const x = rnd ( i ) , y = rnd ( i ) ;
89+ const actual = toArray ( take ( N ) ( iterate ( add ( fromInt ( y ) ) ) ( fromInt ( x ) ) ) ) . map ( toInt ) ;
90+ const expected = Array . from ( { length : N } , ( _ , i ) => x + i * y ) ;
91+ assert . deepEqual ( actual , expected , `after ${ i } tests` ) ;
92+ }
93+ } ) ;
94+ it ( "repeat" , ( ) => {
95+ const N = 10 ;
96+ for ( let i = 1 ; i <= 10 ; i ++ ) {
97+ const x = rnd ( i ) ;
98+ const actual = toArray ( take ( N ) ( repeat ( fromInt ( x ) ) ) ) . map ( toInt ) ;
99+ const expected = Array . from ( { length : N } , ( ) => x ) ;
100+ assert . deepEqual ( actual , expected , `after ${ i } tests` ) ;
101+ }
102+ } ) ;
103+ it ( "cycle" , ( ) => {
104+ const N = 10 ;
105+ for ( let i = 1 ; i <= 10 ; i ++ ) {
106+ const xs = rndNonEmptyArray ( i ) ;
107+ const actual = toArray ( take ( N ) ( cycle ( fromArray ( xs ) ) ) ) . map ( toInt ) ;
108+ const expected = [ ] . concat ( ...Array . from ( { length : N } , ( ) => xs ) ) . slice ( 0 , N ) ;
109+ assert . deepEqual ( actual , expected , `after ${ i } tests` ) ;
110+ }
111+ } ) ;
112+ it ( "replicate" , ( ) => {
113+ for ( let i = 1 ; i <= 10 ; i ++ ) {
114+ const n = rnd ( i ) , x = rnd ( i ) ;
115+ const actual = toArray ( replicate ( fromInt ( n ) ) ( fromInt ( x ) ) ) . map ( toInt ) ;
116+ const expected = Array . from ( { length : n } , ( ) => x ) ;
117+ assert . deepEqual ( actual , expected , `after ${ i } tests` ) ;
118+ }
119+ } ) ;
120+ it ( "unfold" , ( ) => {
121+ for ( let i = 1 ; i <= 10 ; i ++ ) {
122+ const x = rnd ( i ) ;
123+ const actual = toArray ( unfold ( x => ( isZero ( x ) ) ( Some ( Pair ( x ) ( pred ( x ) ) ) ) ( None ) ) ( fromInt ( x ) ) ) . map ( toInt ) ;
124+ const expected = Array . from ( { length : x } , ( _ , i ) => x - i ) ;
125+ assert . deepEqual ( actual , expected , `after ${ i } tests` ) ;
126+ }
127+ } ) ;
83128} ) ;
0 commit comments