KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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!
Tags (comma-separated)
Save Edits
Cancel