Thursday, March 19, 2020

partial functions are only partial implementations

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.


No comments:

Post a Comment

Recent Post

Databricks Delta table merge Example

here's some sample code that demonstrates a merge operation on a Delta table using PySpark:   from pyspark.sql import SparkSession # cre...