Cartesian Product

The cartesian product is the complicated mathematical name for "every combination of multiple sets".

Basically, this is useful (to me) for modeling things like playing cards and dice probabilities.

You can see the code in action in this codepen.

function cartesian(setA, ...sets) {
  let result = [];
  for (const item of setA) {
    result.push([item]);
  }
  if (sets.length > 0) {
    for (const set of sets) {
      result = set.flatMap(itemB => 
             result.map(itemA => [...itemA, itemB])
      );
    }
  }
  return result;
}

// alternate typescript implementation:
function cartesianProduct<T>(...sets: T[][]) {
	let result: T[][] = [[]];
	sets.forEach((set) => {
		result = set.flatMap((item) =>
			result.map((current) => [...current, item])
		);
	});
	return result;
}

// playing cards
log(
  cartesian(   
    ['A','2','3','4','5','6','7','8','9','T','J','Q','K'],
    ['H','D','C','S']
  )
)

// 3d6 (crag dice game)
log(
  cartesian(
    ['1','2','3','4','5','6'],
    ['1','2','3','4','5','6'],
    ['1','2','3','4','5','6'],
  )
)