@@ -25,19 +25,21 @@ const {"insert-by":insertBy,"sort-by":sortBy,reverse} = solution;
25
25
const { "zip-with" :zipWith , zip, unzip} = solution ;
26
26
const { "group-by" :groupBy , "nub-by" :nubBy , "delete-by" :deleteBy , "delete-firsts-by" :deleteFirstsBy } = solution ;
27
27
const { 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 ;
29
29
30
30
const fromInt = LC . fromIntWith ( LC . config ) ;
31
31
const toInt = LC . toIntWith ( LC . config ) ;
32
32
const fromArray = xs => xs . reduceRight ( ( z , x ) => cons ( x ) ( z ) , nil ) ;
33
33
const toArray = foldl ( z => x => [ ...z , x ] ) ( [ ] ) ;
34
34
const fromPair = ( [ fst , snd ] ) => Pair ( fst ) ( snd ) ;
35
35
const toPair = xy => xy ( fst => snd => [ fst , snd ] ) ;
36
+ const fromNullable = x => x === null ? None : Some ( x ) ;
36
37
const toNullable = fn => optX => optX ( null ) ( fn ) ;
37
38
38
39
const rnd = ( m , n = 0 ) => Math . random ( ) * ( n - m ) + m | 0 ;
39
40
const elements = xs => xs [ rnd ( xs . length ) ] ;
40
41
const rndArray = size => Array . from ( { length : rnd ( size ) } , ( ) => rnd ( size ) ) ;
42
+ const rndNonEmptyArray = size => Array . from ( { length : rnd ( size ) || 1 } , ( ) => rnd ( size ) ) ;
41
43
42
44
describe ( "Scott Lists" , function ( ) {
43
45
it ( "nil,cons,singleton" , ( ) => {
@@ -80,4 +82,47 @@ describe("Scott Lists",function(){
80
82
`after ${ i } tests` ) ;
81
83
}
82
84
} ) ;
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
+ } ) ;
83
128
} ) ;
0 commit comments