Skip to content

Instantly share code, notes, and snippets.

@rodobarcaaa
Created October 10, 2023 09:12
Show Gist options
  • Save rodobarcaaa/b05a60362ed0460a82dc40f3291ef24b to your computer and use it in GitHub Desktop.
Save rodobarcaaa/b05a60362ed0460a82dc40f3291ef24b to your computer and use it in GitHub Desktop.
Scala Numbers Examples
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