Skip to content

more testing #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/scott-lists/solution.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ iterate = \ fn x . cons x (iterate fn (fn x))
repeat = \ x . cons x (repeat x) # repeat = Y (S cons)

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

# replicate :: Number -> a -> List a
replicate = \ n . B (take n) repeat
Expand Down
47 changes: 46 additions & 1 deletion tests/scott-lists/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ const {"insert-by":insertBy,"sort-by":sortBy,reverse} = solution;
const {"zip-with":zipWith,zip,unzip} = solution;
const {"group-by":groupBy,"nub-by":nubBy,"delete-by":deleteBy,"delete-firsts-by":deleteFirstsBy} = solution;
const {init,last,tails,inits,slice,transpose} = solution;
const {add,zero} = solution;
const {zero,succ,pred,add,"is-zero":isZero,Pair,None,Some} = solution;

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

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

describe("Scott Lists",function(){
it("nil,cons,singleton",()=>{
Expand Down Expand Up @@ -80,4 +82,47 @@ describe("Scott Lists",function(){
`after ${ i } tests` );
}
});
it("iterate",()=>{
const N = 10;
for ( let i=1; i<=10; i++ ) {
const x = rnd(i), y = rnd(i);
const actual = toArray( take (N) (iterate (add (fromInt(y))) (fromInt(x))) ).map(toInt);
const expected = Array.from( { length: N }, (_,i) => x + i * y );
assert.deepEqual( actual, expected, `after ${ i } tests` );
}
});
it("repeat",()=>{
const N = 10;
for ( let i=1; i<=10; i++ ) {
const x = rnd(i);
const actual = toArray( take (N) (repeat (fromInt(x))) ).map(toInt);
const expected = Array.from( { length: N }, () => x );
assert.deepEqual( actual, expected, `after ${ i } tests` );
}
});
it("cycle",()=>{
const N = 10;
for ( let i=1; i<=10; i++ ) {
const xs = rndNonEmptyArray(i);
const actual = toArray( take (N) (cycle (fromArray(xs))) ).map(toInt);
const expected = [].concat(...Array.from( { length: N }, () => xs )).slice(0,N);
assert.deepEqual( actual, expected, `after ${ i } tests` );
}
});
it("replicate",()=>{
for ( let i=1; i<=10; i++ ) {
const n = rnd(i), x = rnd(i);
const actual = toArray( replicate (fromInt(n)) (fromInt(x)) ).map(toInt);
const expected = Array.from( { length: n }, () => x );
assert.deepEqual( actual, expected, `after ${ i } tests` );
}
});
it("unfold",()=>{
for ( let i=1; i<=10; i++ ) {
const x = rnd(i);
const actual = toArray( unfold ( x => (isZero (x)) (Some (Pair (x) (pred (x)))) (None) ) (fromInt(x)) ).map(toInt);
const expected = Array.from( { length: x }, (_,i) => x-i );
assert.deepEqual( actual, expected, `after ${ i } tests` );
}
});
});