Comment boucle dans Scala
for (i <- 1 to 3) println(i)
Inquisitive Impala
for (i <- 1 to 3) println(i)
for {
i <- 1 to 10
if i < 4
} println(i)
for (i <- 1 to 10 if i < 4) println(i)
val nameMap = Map("firstName" -> "Ed", "lastName" -> "Chigliak")
for ((k,v) <- nameMap) {
println(s"key: $k, value: $v")
}
val result = for ((k,v) <- nameMap) yield {
s"key: $k, value: $v"
}
println(result)
ratings.foreach {
case(movie, rating) => println(s"key: $movie, value: $rating")
}
ratings.foreach(x => println(s"key: ${x._1}, value: ${x._2}"))
ratings.keys.foreach((movie) => println(movie))
ratings.keys.foreach(println)
ratings.values.foreach((rating) => println(rating))
for {
i <- 1 to 10
if i > 3
if i < 6
if i % 2 == 0
} println(i)
val lengths = for (e <- names) yield {
// imagine that this required multiple lines of code
e.length
}
val names = Seq("chris", "ed", "maurice")
val nums = Seq(1, 2, 3)
for (i <- 0 until names.length) {
println(s"$i is ${names(i)}")
}
names.zipWithIndex.foreach { d =>
println(s"${d._2} is ${d._1}")
}