Collect apply given partial function on each element of input collection and it discard elements for which partial function is not defined.
object MyBlog {
def main(args: Array[String]): Unit = {
collectExample()
}
def collectExample(): Unit = {
val divide: PartialFunction[Int, Int] = {
case input: Int if(input != 0) => 100/input
}
val inputList = List(0, 0, 5, 15, 20)
val res = inputList.collect(divide) // List(20, 6, 5) 100/5, 100/15, 100/20
println(res)
}
}
No comments:
Post a Comment