Example # 1: Using isDefinedAt and Apply Function for Partial Function in Scala in Ubuntu 20.04
We have two methods used for the implementation of the partial function. That includes isDefinedAt() and apply() methods. To verify whether the values are within the scope of the function. The apply() method is used to display the deployment of a function.
We have established the scala object with the name “PartialFunctionExample1”. Then, we have defined the variable as “val” which is enclosed inside the scala object “PartialFunctionExample1”. The variable is labeled as “D” and we have declared the partialFunction inside it with the keyword “new”. The new keyword is defined here for the creation of an object and returns an object from the constructor of partialFunction.
The partialFunction constructor takes two inputs of the data type “Int”. The partialFunction is further implemented with two more functions. The “isDefinedAt” has the condition and another is the apply() function that will be performed against the condition specified in the “isDefinedAt()” function. The apply() function is only performed when the condition is satisfied Next, we created the main function definition where the println method is invoked for displaying the outcome of the partial function. Inside the println function, we have called the variable “D” and set the value for it as “5”.
The following outcome is generated upon the compilation of the above scala program:
Example 2: Using the orElse Function for Partial Function in Scala in Ubuntu 20.04
We can chain the partial functions using the orElse function. Below, we have the execution of the orElse function.
We have built the object which is labeled as “PartialFunctionExample2”. Inside it, we have specified two partialFunction which are called under the variables “C1” and “C2”. This time the partial function constructor takes different data types as input. We have passed the “Int” and “String” data types. The partial function called inside the “C1” has the case condition for the odd value and the partial function inside the “C2” variable has been set for the even case condition.
After implementing the conditions for partial function, we have declared another variable “Result” where the method “orElse” is employed between the variable “C1” and “C2”. Now, we have chained the partial function with the “orElse” method. The main definition is invoked for running the above declarations. Here, we have set the value “152” for the “n1” variable which is used in the first partial function. With the println function, the variable “Result” sets the “n1” variable as input for being performed by the “orElse” method.
The value we have assigned is “152” which is an odd number. Also, the compiler executed the case set for the odd values.
Example 3: Using the “andThen” Function for Partial Function in Scala in Ubuntu 20.04
The partial function further provides the “andThen”. With the andThen method, we can append the values of the nested partial functions.
We have an object “PartialExample3” and inside its block, we have a definition of the main method. For the main function, we have set the variable with the built-in modifier “val”. The val is assigned a name as “V1”. We have called the PartialFunction and created its constructor for passing the “Int” data types. Within the partial function body, we have a case expression that is to be satisfied.
Then, we have declared the “AppenIt” variable and it is also assigned a function. Next, we have chained the partialFunction of “V1” and the function provided to “AppendIt” with the method “andThen”. The “andThen” method appends the results of both functions and then returns the output value.
We have the appended value obtained from the partial function “andThen” method below.
Example 4: Using the Case Expression for Partial Function in Scala in Ubuntu 20.04
We have the case expression that utilizes the binary operator for the partial function. Consider the following case expression program for partial function:
The object here is set with the name “PartialFunctionExample4” where the program’s main definition is constructed. Then, we have a variable “PF” created with the “val” keyword. The partial function is utilized inside the variable “PF” and the partial function has the case expression, The case expression is specified with the binary operator. The “PF” is called in the println function and set to the number “200”. Here, we don’t need any other method as the partial function is implemented with the case expression.
As the condition is met for the case expression, the output is executed like this:
Example 5: Using the Partial Function for Conversion of a Function in Scala in Ubuntu 20.04
We cannot change the definition of the particular function. However, a partial function can be used in its replacement. The partial function called the specified function and converted into the new function. These statements will be cleared in the following program:
We have constructed the object as “PartialFunctionExample5”. The object block is defined with the simple function “ApplyDivision” where division operation is done for the number “200”. After that, we created another function but as a partial function. To the partial function, we have assigned the isDefinedAt method and Apply method which is discussed in the very first example. Here, the function “ApplyDivision” is replaced with the partial function. We have invoked the function “ApplyDivison” inside the partial function.
The simple conversion of the normal function to the partial function returned the following value:
The subject of the article is Scala partial function. Also, we have complied with the methods which are supported by the partial function in Scala. The isDefined and apply methods are explained in the first example. We have chained the partial function with the orElse and andThen methods in the next examples. Furthermore, we have converted the function into a partial function.