Build a new list with a for-comprehension.

For-Yield

ForYield.scala
object Main {
  def main(args: Array[String]): Unit = {
    val factor = 
    val doubled = for (n <- List(1, 2, 3)) yield n * factor
    val text = doubled.mkString(",")

    println("count=" + doubled.length)
    println("text=" + text)
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    val factor = 
    val doubled = for (n <- List(1, 2, 3)) yield n * factor
    val text = doubled.mkString(",")

    println("count=" + doubled.length)
    println("text=" + text)
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    val factor = 
    val doubled = for (n <- List(1, 2, 3)) yield n * factor
    val text = doubled.mkString(",")

    println("count=" + doubled.length)
    println("text=" + text)
  }
}
for-yield A `for` with `yield` runs over each element and collects the results into a new list, the same result as `map`.