Scala: Partial functions andThen

val seqOne: Seq[Int] = Seq(1,2,3,4,5,6)val oddThrice = new PartialFunction[Int, Int] {
def isDefinedAt(x : Int) = x % 2 == 1
def apply(x : Int) = x * 3
}
val oddSevenTimes = new PartialFunction[Int,Int] {
def isDefinedAt(x : Int) = x % 2 == 1
def apply(x : Int) = x * 7
}
val transformer = oddThrice.andThen(oddSevenTimes)
println(seqOne)
println(seqOne.map(transformer))
List(1, 2, 3, 4, 5, 6)
List(21, 42, 63, 84, 105, 126)

--

--

--

On a continuing learning journey..

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

Recommended from Medium

Decrypt & Encrypt Data using Nodejs

Replication MongoDB

Read/Write JSON Files with Node.js

5 Things You Should Know About Navigating create-react-app

Managing React State with Proxily

My Article List

Using Web Technologies to create your first Cross-Platform Desktop Application with Electron

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: Either

Polymorphic functions in Scala

Comparing Traits in Rust to Type Classes in Scala

Type Classes in Scala 2