Why doesn't accept the Map constructor an Array of iterable tuples?

Viewed 348

I created a tuple type in Javascript that implements the Iterable protocol. Now it should be easy to convert to Map:

const Tuple = (...args) => {
  const Tuple = f => f(...args);
  Tuple[Symbol.iterator] = () => args[Symbol.iterator]();
  return Tuple;
};

new Map([Tuple(1, "a"), Tuple(2, "b")]); // Map {undefined => undefined}

Instead I have to do the conversion manually:

const Tuple = (...args) => {
  const Tuple = f => f(...args);
  Tuple[Symbol.iterator] = () => args[Symbol.iterator]();
  return Tuple;
};

const toArray = tx => tx((...args) => args);

const Map_ = (...pairs) => {
  return new Map(pairs.map(pair => toArray(pair)));
};

const map = Map_(Tuple(1, "a"), Tuple(2, "b"));

console.log(map.get(1), map.get(2)); // "a" "b"

It seems as if Map only accepts an Iterable for the outer composite type, not for the inner pairs. What am I missing? Is there another way to achieve the conversion?

3 Answers
Related