Consider such a map:
Map("one" -> Iterable(1,2,3,4), "two" -> Iterable(3,4,5), "three" -> Iterable(1,2))
I want to get a list of all possible permutations of elements under Iterable, one element for each key. For this example, this would be something like:
// first element of "one", first element of "two", first element of "three"
// second element of "one", second element of "two", second element of "three"
// third element of "one", third element of "two", first element of "three"
// etc.
Seq(Iterable(1,3,1), Iterable(2,4,2), Iterable(3,5,1),...)
What would be a good way to accomplish that?
If you want every combination:
If you want each iterable to march up together, but stop when the shortest is exhuasted:
If you want each iterable to wrap around but stop when the longest one has been exhausted:
Edit: If you want arbitrary numbers of input lists, then you’re best off with recursive functions. For Cartesian products:
and to replace
zippedyou want something like:You can try these out with
cart(m.values),zipper(m.values), andzipper(m.values.map(icf)).