iterate.js 543 B

12345678910111213141516
  1. // Map values in an object
  2. const mapValues = (obj, fn) => Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, fn(v)]));
  3. // Array of number or bigint
  4. const max = (...values) => values.slice(1).reduce((x, y) => (x > y ? x : y), values[0]);
  5. const min = (...values) => values.slice(1).reduce((x, y) => (x < y ? x : y), values[0]);
  6. // Cartesian product of a list of arrays
  7. const product = (...arrays) => arrays.reduce((a, b) => a.flatMap(ai => b.map(bi => [...ai, bi])), [[]]);
  8. module.exports = {
  9. mapValues,
  10. max,
  11. min,
  12. product,
  13. };