All files index.js

89.16% Statements 74/83
82.5% Branches 33/40
94.74% Functions 18/19
88.89% Lines 72/81

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150    1x 21x 61x 21x     40x   21x     1x 529x 172x     357x 333x     24x     1x 165x   3x     3x   3x       1x 43x 41x     1x 43x 43x     1x 59x 59x     1x 6x 6x     1x 26x 19x     19x     19x     7x 1x     6x 19x 19x 19x 6x     13x     6x     1x                     1x 3x 3x 1x     6x 2x     1x 2x 4x     1x     1x 4x 1x   3x     1x 4x 4x 1x   3x     1x 5x 5x 1x   4x     1x 5x 1x     4x              
import * as pairs from 'pairs-js';
 
const cons = (...args) => {
  const iter = (elements) => {
    if (!elements.length) {
      return null;
    }
 
    return pairs.cons(elements[0], iter(elements.slice(1)));
  };
  return iter(args);
};
 
const isList = (list) => {
  if (list === null) {
    return true;
  }
 
  if (pairs.isPair(list)) {
    return isList(pairs.cdr(list));
  }
 
  return false;
};
 
const checkList = (list) => {
  if (!isList(list)) {
    let value;
    Iif (typeof list === 'object') {
      value = JSON.stringify(list, null, 2);
    } else {
      value = String(list);
    }
    throw new Error(`Argument must be list, but it was '${value}'`);
  }
};
 
const head = (list) => {
  checkList(list);
  return pairs.car(list);
};
 
const tail = (list) => {
  checkList(list);
  return pairs.cdr(list);
};
 
const isEmpty = (list) => {
  checkList(list);
  return list === null;
};
 
const push = (element, list) => {
  checkList(list);
  return pairs.cons(element, list);
};
 
const toString = (list) => {
  if (!isList(list)) {
    Iif (pairs.isPair(list)) {
      return `pair: ${pairs.toString(list)}`;
    }
    Iif (typeof list === 'object') {
      return JSON.stringify(list, null, 2);
    }
    return list;
  }
 
  if (isEmpty(list)) {
    return '()';
  }
 
  const rec = (p) => {
    const first = head(p);
    const rest = tail(p);
    if (isEmpty(rest)) {
      return toString(first);
    }
 
    return `${toString(first)}, ${rec(rest)}`;
  };
 
  return `(${rec(list)})`;
};
 
const has = (value, list) => {
  checkList(list);
  if (isEmpty(list)) {
    return false;
  }
  if (head(list) === value) {
    return true;
  }
  return has(value, tail(list));
};
 
const count = (list) => {
  checkList(list);
  if (isEmpty(list)) {
    return 0;
  }
 
  const iter = (elements, n) => (isEmpty(elements) ? n : iter(tail(elements), n + 1));
  return iter(list, 0);
};
 
const reverse = (list) => {
  checkList(list);
  const iter = (curElements, acc) => (isEmpty(curElements)
    ? acc
    : iter(tail(curElements), pairs.cons(head(curElements), acc)));
  return iter(list, cons());
};
 
const concat = (list1, list2) => {
  if (isEmpty(list1)) {
    return list2;
  }
  return pairs.cons(head(list1), concat(tail(list1), list2));
};
 
const map = (f, list) => {
  checkList(list);
  if (isEmpty(list)) {
    return cons();
  }
  return push(f(head(list)), map(f, tail(list)));
};
 
const filter = (f, list) => {
  checkList(list);
  if (isEmpty(list)) {
    return cons();
  }
  return f(head(list)) ? push(head(list), filter(f, tail(list))) : filter(f, tail(list));
};
 
const reduce = (f, acc, elements) => {
  if (isEmpty(elements)) {
    return acc;
  }
 
  return reduce(f, f(head(elements), acc), tail(elements));
};
 
export {
  cons, head, tail, isEmpty, toString, has, count, reverse, isList, push,
  concat, map, filter, reduce,
};