Scala: Partial function argument in collect api

val seqOne: Seq[Int] = Seq(1,2,3,4,5,6)
val resultOne = seqOne.filter(_%2==0).map(_*2)
println(resultOne)
List(4, 8, 12)
val doubleIfEven = new PartialFunction[Int,Int]  {
def isDefinedAt(x : Int) = x % 2 == 0
def apply(x : Int) = x * 2
}
val result = seqOne.collect(doubleIfEven)
println(result)
List(4, 8, 12)

--

--

--

On a continuing learning journey..

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Browersify: Use require() in Client Side JavaScript

Setting up a Node.js Environment

JS Coding 101: COVID-19 Tracker Application with React JS

Build a “auto-resize” Carousel can fix to a certain item by react

10 Most incredible questions when facing a job interview

How to detect a sequence of bytes, in a byte array, using client-side Javascript

Inheritance: Classy Prototype Chains

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Learn

Learn

On a continuing learning journey..

More from Medium

Scala: flatMap and for-comprehension

Reactive Web Application: With Play, Akka, and Reactive Streams — Part 1

Polymorphic functions in Scala

Learn Functional Programming With Me — Part 1