Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at a Scala program to separate even and odd numbers in a List of Integers.
We can achieve this using “partition” function defined on List. partition function takes a predicate as parameter and separates the list values into two groups i.e., elements that are satisfying the predicates and the elements that are not.
Let’s look at the program:
1 2 3 4 5 6 7 8 9 |
object PartitionFunction extends App { val numbers = List.range(1, 6) val evenOdds = numbers partition (_ % 2 == 0) println("Even and Odds: " + evenOdds) println("Evens: " + evenOdds._1) // evens println("Odds: " + evenOdds._2) // odds } |
Output:
Happy learning.
Please leave a reply in case of any queries.