Skip to content

Commit f2720de

Browse files
authored
Merge pull request #54 from JohanWiltink/main
bugfix lc.js
2 parents 904147f + 7a902f9 commit f2720de

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/lambda-calculus.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ function evalLC(term) {
340340
// callback function which will evaluate term.body in an env with the input
341341
function result(arg) {
342342
let argEnv;
343-
if ( arg.term && arg.env ) ({ term: arg, env: argEnv } = arg); // if callback is passed another callback, or a term
343+
if ( arg?.term && arg?.env ) ({ term: arg, env: argEnv } = arg); // if callback is passed another callback, or a term
344344
const termVal = new Tuple( typeof arg !== 'number' ? arg : fromInt(arg) , new Env(argEnv) );
345345
return runEval( new Tuple(term.body, new Env(env).setThunk(term.name, termVal)), stack );
346346
}

tests/scott-lists/test.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const {"take-while":takeWhile,"drop-while":dropWhile,"drop-while-end":dropWhileE
2121
const {"split-at":splitAt,get,set} = solution;
2222
const {any,all,find,"find-indices":findIndices,"find-index":findIndex} = solution;
2323
const {partition,span,"minimum-by":minimumBy,"maximum-by":maximumBy} = solution;
24-
const {"insret-by":insertBy,"sort-by":sortBy,reverse} = solution;
24+
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;
@@ -31,13 +31,14 @@ 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] ) ([]) ;
34+
const fromPair = ([fst,snd]) => Pair(fst)(snd) ;
35+
const toPair = xy => xy ( fst => snd => [fst,snd] ) ;
36+
const toNullable = fn => optX => optX (null) (fn) ;
3437

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

39-
const refReplicate = length => x => Array.from( { length }, () => x ) ;
40-
4142
describe("Scott Lists",function(){
4243
it("nil,cons,singleton",()=>{
4344
assert.deepEqual( toArray( nil ), [] );
@@ -56,4 +57,27 @@ describe("Scott Lists",function(){
5657
assert.deepEqual( toArray( scanl (add) (zero) (fromArray(xs.map(fromInt))) ).map(toInt), xs.reduce( (z,x) => [ ...z, z[z.length-1]+x ] , [0] ), `after ${ i } tests` );
5758
}
5859
});
60+
it("take,drop",()=>{
61+
for ( let i=1; i<=10; i++ ) {
62+
const n = rnd(i), xs = rndArray(i);
63+
assert.deepEqual( toArray( take (fromInt(n)) (fromArray(xs.map(fromInt))) ).map(toInt), xs.slice(0,n), `after ${ i } tests` );
64+
assert.deepEqual( toArray( drop (fromInt(n)) (fromArray(xs.map(fromInt))) ).map(toInt), xs.slice(n), `after ${ i } tests` );
65+
}
66+
});
67+
it("append,concat,snoc",()=>{
68+
for ( let i=1; i<=10; i++ ) {
69+
const x = rnd(i), xs = rndArray(i), ys = rndArray(i);
70+
assert.deepEqual( toArray( append (fromArray(xs.map(fromInt))) (fromArray(ys.map(fromInt))) ).map(toInt), [...xs,...ys], `after ${ i } tests` );
71+
assert.deepEqual( toArray( concat (fromArray([ fromArray(xs.map(fromInt)), fromArray(ys.map(fromInt)) ])) ).map(toInt), [...xs,...ys], `after ${ i } tests` );
72+
assert.deepEqual( toArray( snoc (fromArray(xs.map(fromInt))) (fromInt(x)) ).map(toInt), [...xs,x], `after ${ i } tests` );
73+
}
74+
});
75+
it("uncons",()=>{
76+
for ( let i=1; i<=10; i++ ) {
77+
const xs = rndArray(i);
78+
assert.deepEqual( toNullable ( xy => toPair(xy).map( (x,i) => i ? toArray(x).map(toInt) : toInt(x) ) ) ( uncons (fromArray(xs.map(fromInt))) ),
79+
xs.length ? [ xs[0], xs.slice(1) ] : null ,
80+
`after ${ i } tests` );
81+
}
82+
});
5983
});

0 commit comments

Comments
 (0)