Created
October 10, 2023 09:12
-
-
Save rodobarcaaa/b05a60362ed0460a82dc40f3291ef24b to your computer and use it in GitHub Desktop.
Scala Numbers Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Numbers extends App { | |
// Podemos inicializamos una lista y al mismo tiempo le pre-populamos valores | |
val numbers = List(1, 2, 3, 4, 5) | |
// Recorremos la lista de números y filtramos los pares | |
def pairNumbers: List[Int] = | |
for { | |
number <- numbers | |
if number % 2 == 0 | |
} yield number | |
// Imprimimos el resultado separados por ", " | |
println(pairNumbers.mkString(", ")) // 2, 4 | |
//Plus: Ejemplo similar usando filter para los impares | |
println(numbers.filter(_ % 2 != 0).mkString(", ")) // 1, 3, 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment