All files index.js

100% Statements 28/28
100% Branches 8/8
100% Functions 10/10
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 431x 8x 4x 4x     22x   1x 14x 4x 4x       1x 7x 4x     1x 5x 4x     1x 2x 2x 8x 5x   3x 3x 3x     2x            
const cons = (a, b) => {
  const pair = f => f(a, b);
  pair.isPair = true;
  return pair;
};
 
const isPair = pair => typeof pair === 'function' && pair.isPair === true;
 
const checkPair = (pair) => {
  if (!isPair(pair)) {
    const value = typeof pair === 'object' ? JSON.stringify(pair, null, 2) : String(pair);
    throw new Error(`Argument must be pair, but it was '${value}'`);
  }
};
 
const car = (pair) => {
  checkPair(pair);
  return pair(a => a);
};
 
const cdr = (pair) => {
  checkPair(pair);
  return pair((a, b) => b);
};
 
const toString = (pair) => {
  checkPair(pair);
  const iter = (p) => {
    if (!isPair(p)) {
      return String(p);
    }
    const left = car(p);
    const right = cdr(p);
    return `(${iter(left)}, ${iter(right)})`;
  };
 
  return iter(pair);
};
 
export {
  cons, car, cdr, isPair, toString, checkPair,
};