Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: why does a `for` comprehension on a Map sometimes yield a List?

Tags:

scala

Why, in the below code example, does isAList's for comprehension yield a List, but the other two yield Maps? I can't think of any reason - the only difference seems to be that there is isAList's comprehension declares two variables, and the others declare one or zero.

object Weird {
  def theMap: Map[Int, String] =
    Map(1 -> "uno", 2 -> "dos", 3 -> "tres")

  def main(args: Array[String]) {

    val isAMap = for {
      (key, value) <- theMap
    } yield (key*2 -> value*2)

    val isAlsoAMap = for {
      (key, value) <- theMap
      doubleKey = key*2
    } yield (doubleKey -> value*2)

    val isAList = for {
      (key, value) <- theMap    
      doubleKey = key*2
      doubleValue = value*2
    } yield (doubleKey -> doubleValue)

    println(isAMap)
    println(isAlsoAMap)
    println(isAList)
  }
}

outputs

Map(2 -> unouno, 4 -> dosdos, 6 -> trestres)
Map(2 -> unouno, 4 -> dosdos, 6 -> trestres)
List((2,unouno), (4,dosdos), (6,trestres))

I am comparatively new to Scala, so apologies if I'm being incredibly naive about something!

like image 292
S M Avatar asked Jun 29 '13 06:06

S M


People also ask

How does for comprehension work in Scala?

Scala offers a lightweight notation for expressing sequence comprehensions. Comprehensions have the form for (enumerators) yield e , where enumerators refers to a semicolon-separated list of enumerators. An enumerator is either a generator which introduces new variables, or it is a filter.

What operations is a for comprehension syntactic sugar for?

In short - a for comprehension gives us a syntactic sugar for working with large, sequenced and/or nested computations using flatMap , map and filter .


1 Answers

Recently discussed on the ML:

https://groups.google.com/forum/#!msg/scala-internals/Cmh0Co9xcMs/D-jr9ULOUIsJ

https://issues.scala-lang.org/browse/SI-7515

Suggested workaround is to use a tuple to propagate the variables.

scala> for ((k,v) <- theMap; (dk,dv) = (k*2,v*2)) yield (dk,dv)
res8: scala.collection.immutable.Map[Int,String] = Map(2 -> unouno, 4 -> dosdos, 6 -> trestres)

More on the tupling mechanism:

What are the scoping rules for vals in Scala for-comprehensions

like image 171
som-snytt Avatar answered Oct 19 '22 18:10

som-snytt