title: Cartesian product of two lists
body: <p>Given a map where a digit is associated to several characters</p>
  
  <pre><code>scala&gt; val conversion = Map("0" -&gt; List("A", "B"), "1" -&gt; List("C", "D"))
  conversion: scala.collection.immutable.Map[java.lang.String,List[java.lang.String]] =
    Map(0 -&gt; List(A, B), 1 -&gt; List(C, D))
  </code></pre>
  
  <p>I want to generate all possible character sequences based on a sequence of digits. Examples:</p>
  
  <pre><code>"00" -&gt; List("AA", "AB", "BA", "BB")
  "01" -&gt; List("AC", "AD", "BC", "BD")
  </code></pre>
  
  <p>I can do this with for comprehensions</p>
  
  <pre><code>scala&gt; val number = "011"
  number: java.lang.String = 011
  </code></pre>
  
  <p>Create a sequence of possible characters per index</p>
  
  <pre><code>scala&gt; val values = number map { case c =&gt; conversion(c.toString) }
  values: scala.collection.immutable.IndexedSeq[List[java.lang.String]] =
    Vector(List(A, B), List(C, D), List(C, D))
  </code></pre>
  
  <p>Generate all the possible character sequences</p>
  
  <pre><code>scala&gt; for {
       | a &lt;- values(0)
       | b &lt;- values(1)
       | c &lt;- values(2)
       | } yield a+b+c
  res13: List[java.lang.String] = List(ACC, ACD, ADC, ADD, BCC, BCD, BDC, BDD)
  </code></pre>
  
  <p>Here things get ugly and it will only work for sequences of three digits. Is there any way to achieve the same result for any sequence length?</p>