Scala - Matching on Sequences Code Example

The code for Matching on Sequences

val list1 = List(10, 20, 20, 40)
val list2 = List(11, 22, 33)

val empty = List()
for (l <- List(list1, list2, empty)) {
 l match {
 case List(_, 20, _, _) => println("Found with the 2nd being 20.")
 case List(_*) => println("Other list.")
 }
}

/*
Output:
Found with the 2nd being 20.
Other list.
Other list.
*/
Code by Abhishek Shrivastava, on August 7, 2022 18:10

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.