“Bagaimana Loop di Scala” Kode Jawaban

Bagaimana Loop di Scala

for (i <- 1 to 3) println(i)
Inquisitive Impala

Bagaimana Loop di Scala

for {
    i <- 1 to 10
    if i < 4
} println(i)
Inquisitive Impala

Bagaimana Loop di Scala

for (i <- 1 to 10 if i < 4) println(i)
Inquisitive Impala

Bagaimana Loop di Scala

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)
Inquisitive Impala

Bagaimana Loop di Scala

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))
Inquisitive Impala

Bagaimana Loop di Scala

for {
    i <- 1 to 10
    if i > 3
    if i < 6
    if i % 2 == 0
} println(i)
Inquisitive Impala

Bagaimana Loop di Scala

val lengths = for (e <- names) yield {
    // imagine that this required multiple lines of code
    e.length
}
Inquisitive Impala

Bagaimana Loop di Scala

val names = Seq("chris", "ed", "maurice")
val nums = Seq(1, 2, 3)
Inquisitive Impala

Bagaimana Loop di Scala

for (i <- 0 until names.length) {
    println(s"$i is ${names(i)}")
}
Inquisitive Impala

Bagaimana Loop di Scala

names.zipWithIndex.foreach { d =>
    println(s"${d._2} is ${d._1}")
}
Inquisitive Impala

Jawaban yang mirip dengan “Bagaimana Loop di Scala”

Pertanyaan yang mirip dengan “Bagaimana Loop di Scala”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya