2

Possible Duplicate:
Scala - can yield be used multiple times with a for loop?
Cartesian product of two lists

I try to combine some Scala Lists the following way, but no success so far.

List (List ("abc", "def"), List ("gh", "ij", "kl"), List ("mnop") ...)

-> The result should be

List(
List("abc", "gh", "mnop"),
List("abc", "ij", "mnop"),
List("abc", "kl", "mnop"),
List("def", "gh", "mnop"),
List("def", "ij", "mnop"),
List("def", "kl", "mnop")
)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Rob
  • 191
  • 1
  • 1
  • 7
  • 1
    This is called a [cartesian product](http://en.wikipedia.org/wiki/Cartesian_product). There's an example of how to do it in Scala [in this answer](http://stackoverflow.com/a/8218167/98117). – hammar Oct 31 '12 at 00:26
  • Or here: http://stackoverflow.com/a/5177163/312172 – user unknown Oct 31 '12 at 04:07

1 Answers1

2
for {
  a <- List("abc","def")
  b <- List("gh", "ij", "kl")
  c <- List("mnop")
} yield List(a, b, c)
Steve Waldman
  • 13,689
  • 1
  • 35
  • 45