Labels

Thursday, April 14, 2022

Higher Order Functions (HOF) in Scala (Class -34)

 A Function that takes a function as a parameter is referred to as HOF. In other words, passing a function to another function is called Higher Order Function.

In Scala higher-order functions are functions that do at least one of the following (and may do both):

• Takes one or more functions as arguments (parameters).

• Return as output a function.


Example:

                 def f1 = println(“I am god”)

                 def f2(f:Unit) = f

                  f2(f1)

 Here f1 is default first class function.

   If f2 can take f1 as an argument, then f2 is HOF.

The key to understanding how higher-order functions are defined is to understand that a function definition defines a function type. That is a type that takes Zero or a set of parameters (of given types) and returns a result. Thus the function:

(x : Int) => x * 2

Is a one parameter function that takes an Int and returns an Int—that is its type? Thus the

                                     (Parametertypelist) = > return type



3 Parameters of Higher Order Function:




Uses of HOF:

a)      Reduces the complexity of the code i.e. reduces the no. of lines of code for easy to understand.

b)      Easy debugging.

c)       Less maintenance.

Because of above advantages, nowadays data engineers prefer Scala language instead of Java language in executing projects. 

Types of Higher Order Functions (HOF) using in Scala:

1)      Foreach

2)      Map

3)      FlatMap

4)      Filter

5)      Reduce

1)      Foreach: used to print the output of Unit datatype.

                                foreach(println)

                    collection[A].foreach (f: A) => Unit)







2) Map: One to One Function. When Input and Output is one and same, we use Map HOF.

                     collection [A].map (f: (A) => B)



1)    3)   Filter: For Yield with Iterator. We use when no. of input elements will be more and output collections will be less.


1)      4) Reduce: To consolidate collection to a single value / element and finally will create Shuffle.

                                Reduce will always give single output.






1)      5) Flatmap: to unnest the data. 1 * n




Independent Function: Functions should be given with Parenthesis and will have logic.

 




Anonymous Functions: Functions without a name, only object which is also known as function literal. This type of functions is used when the user wants to create an inline function.


Nested Function: Function within another function. nested function is defined as a function which is defined inside the definition of another function. Programming languages like c, java, etc., do not support nested functions but Scala does. 

                                Def f1 à Main Function

                                Def f2 à Nested Function


VarArg Function: Function having Variable Arguments.




Recursion function is a function that calls itself again and again until a specific condition becomes true. 

Most programs that require adding or doing some specific calculations multiple times use recursion. It is a better choice instead of an infinite while loop that need a break condition. 

Partially applied functions, are actually those function that allows you to implement function calls with partial arguments i.e. using only a few values in a function call and uses rest values form a common call initiation.

No comments:

Post a Comment