The differential between "for (elm <- myList) gives f (_)" and "myList map f (_)" in Scala

Why do different strings give different return values?

val tagIds = postData._1 map (TagTable.newTag(_))
// tagIds is defined as val tagIds: Array[Long]

and

val tagIds = for(tag <- postData._1) yield TagTable.newTag(_)
// tagIds is defined as val tagIds: Array[models.UnsavedTag => Long]
+3
source share
2 answers

Due to a simple input error:

val tagIds = for(tag <- postData._1) yield TagTable.newTag(tag)
                                                           ^^^
+4
source
val tagIds = postData._1 map (TagTable.newTag(_))

This line says that each element is tagcontained in the collection postData._1and calls TagTable.newTag(tag). Then it tagIdsis a set containing all the results of these calls.

val tagIds = for(tag <- postData._1) yield TagTable.newTag(_)

tag, postData._1, TagTable.newTag(_) ( x => TagTable.newTag(x)). tagIds , .

, . :

val tagIds = for(tag <- postData._1) yield TagTable.newTag(tag)
+3

All Articles