Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 30c73be

Browse files
authoredFeb 16, 2022
Merge pull request #62 from JohanWiltink/main
more testing
2 parents 489c50f + 005592b commit 30c73be

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed
 

‎tests/scott-lists/solution.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ iterate = \ fn x . cons x (iterate fn (fn x))
116116
repeat = \ x . cons x (repeat x) # repeat = Y (S cons)
117117

118118
# cycle :: List a -> List a
119-
cycle = \ xs . xs () (concat (repeat xs))
119+
cycle = \ xs . xs () \ _x _xs . concat (repeat xs)
120120

121121
# replicate :: Number -> a -> List a
122122
replicate = \ n . B (take n) repeat

‎tests/scott-lists/test.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,21 @@ const {"insert-by":insertBy,"sort-by":sortBy,reverse} = solution;
2525
const {"zip-with":zipWith,zip,unzip} = solution;
2626
const {"group-by":groupBy,"nub-by":nubBy,"delete-by":deleteBy,"delete-firsts-by":deleteFirstsBy} = solution;
2727
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;
2929

3030
const fromInt = LC.fromIntWith(LC.config);
3131
const toInt = LC.toIntWith(LC.config);
3232
const fromArray = xs => xs.reduceRight( (z,x) => cons(x)(z) , nil ) ;
3333
const toArray = foldl ( z => x => [...z,x] ) ([]) ;
3434
const fromPair = ([fst,snd]) => Pair(fst)(snd) ;
3535
const toPair = xy => xy ( fst => snd => [fst,snd] ) ;
36+
const fromNullable = x => x===null ? None : Some(x) ;
3637
const toNullable = fn => optX => optX (null) (fn) ;
3738

3839
const rnd = (m,n=0) => Math.random() * (n-m) + m | 0 ;
3940
const elements = xs => xs[ rnd(xs.length) ] ;
4041
const rndArray = size => Array.from( { length: rnd(size) }, () => rnd(size) ) ;
42+
const rndNonEmptyArray = size => Array.from( { length: rnd(size) || 1 }, () => rnd(size) ) ;
4143

4244
describe("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

Comments
 (0)
Please sign in to comment.