Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial Functions in Scala

Tags:

scala

I just wanted to clarify something about partially defined functions in Scala. I looked at the docs and it said the type of a partial function is PartialFunction[A,B], and I can define a partial function such as

val f: PartialFunction[Any, Int] = {...}

I was wondering, for the types A and B, is A a parameter, and B a return type? If I have multiple accepted types, do I use orElse to chain partial functions together?

like image 609
jstnchng Avatar asked Aug 19 '15 01:08

jstnchng


People also ask

What is partial function in Scala?

When a function is not able to produce a return for every single variable input data given to it then that function is termed as Partial function. It can determine an output for a subset of some practicable inputs only. It can only be applied partially to the stated inputs.

What is partial function example?

(definition) Definition: A function which is not defined for some inputs of the right type, that is, for some of a domain. For instance, division is a partial function since division by 0 is undefined (on the Reals).

What are partial functions used for?

What can partial functions do for us? A partial function allows us to call a second function with fixed values in certain arguments. For instance, we may have a function that computes an exponentiation. Then, we may need to create a new function that assigns a fixed value to either the base or the exponent.

What is higher order functions in Scala?

In Scala, a higher-order function is a function which takes another function as an argument. A higher-order function describes "how" the work is to be done in a collection. Let's learn the higher order function map. The map applies the function to each value in the collection and returns a new collection.


1 Answers

In the set theoretic view of a function, if a function can map every value in the domain to a value in the range, we say that this function is a total function. There can be situations where a function cannot map some element(s) in the domain to the range; such functions are called partial functions.

Taking the example from the Scala docs for partial functions:

val isEven: PartialFunction[Int, String] = {
  case x if x % 2 == 0 => x+" is even"
}

Here a partial function is defined since it is defined to only map an even integer to a string. So the input to the partial function is an integer and the output is a string.

val isOdd: PartialFunction[Int, String] = {
  case x if x % 2 == 1 => x+" is odd"
}

isOdd is another partial function similarly defined as isEven but for odd numbers. Again, the input to the partial function is an integer and the output is a string.

If you have a list of numbers such as:

List(1,2,3,4,5)

and apply the isEven partial function on this list you will get as output

List(2 is even, 4 is even)

Notice that not all the elements in the original list have been mapped by the partial function. However, there may be situations where you want to apply another function in those cases where a partial function cannot map an element from the domain to the range. In this case we use orElse:

val numbers = sample map (isEven orElse isOdd)

And now you will get as output:

List(1 is odd, 2 is even, 3 is odd, 4 is even, 5 is odd)
like image 134
M.K. Avatar answered Oct 02 '22 10:10

M.K.