Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Partial Functions in Scala

DZone's Guide to

Partial Functions in Scala

Interested in learning some of the finer points of the Scala language? Read on to get a quick overview of Scala's partial functions, and how they aid in development.

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

As the name suggests, partial functions are only partial implementations. They do not cover every possible scenario of incoming parameters. A partial function caters to only a subset of possible data for which it has been defined. In order to assist developers, if the partial function is defined for a given input, Scala's PartialFunction trait provides the isDefinedAt method. The isDefinedAt method can be queried if it can handle a given value.

Partial functions in Scala can be defined by using the case statement. Let us define a simple partial function, squareRoot. The function would take in a double input parameter and would return the square root.

val squareRoot: PartialFunction[Double, Double] = { 
    case d: Double if d > 0 => Math.sqrt(d) 
} 

As is evident from the above example, we are not aware what would happen if d is less than 0.

Advantages

Consider this list of numbers having some values.

val list: List[Double] = List(4, 16, 25, -9)


If I use a simple map function with Math.sqrt()then I'll get an annoying NaN at the end of my result list.

val result = list.map(Math.sqrt)
result: List[Double] = List(2.0, 4.0, 5.0, NaN)


We never intended to have a NaN value in our result. What could be worse? We could have got an exception.

Let us try to use our previously defined squareRoot partial function along with collect.

val result = list.collect(squareRoot)
result: List[Double] = List(2.0, 4.0, 5.0)


And this time, we can observe that we do not have any unwanted elements in our result list. Thus, partial functions can help us to get rid of any side effects.

There are other helpful functions such as orElse and andThen that can be used with partial functions. Please refer to this video if you are looking for few more hands-on examples of partial functions in Scala.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
scala ,functional programming ,java ,partial functions

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}