Open In App

Higher Order Functions in Scala

Last Updated : 15 Apr, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

A function is called Higher Order Function if it contains other functions as a parameter or returns a function as an output i.e, the functions that operate with another functions are known as Higher order Functions. It is worth knowing that this higher order function is applicable for functions and methods as well that takes functions as parameter or returns a function as a result. This is practicable as the compiler of Scala allows to force methods into functions.
Some important points about Higher order functions:

  • The Higher order functions are possible, as Scala programming language acts towards the functions as first-class values, which implies that analogous to some other values, functions can even be passed as a parameter or can be returned as an output, which is helpful in supplying an adjustable method for writing codes.
  • It is beneficial in producing function composition where, functions might be formed from another functions. The function composition is the method of composing where a function shows the utilization of two composed functions.
  • It is also constructive in creating lambda functions or anonymous functions. The anonymous functions are the functions which does not has name, though perform like a function.
  • It is even utilized in minimizing redundant lines of code from a program.

Now, lets see some examples.

  • Example :




    // Scala program of higher order
    // function
      
    // Creating object
    object GfG 
    {
          
        // Main method
        def main(args: Array[String]) 
        {
            // Displays output by assigning 
            // value and calling functions
            println(apply(format, 32))
          
        }
          
        // A higher order function
        def apply(x: Double => String, y: Double) = x(y)
      
        // Defining a function for
        // the format and using a
        // method toString()
        def format[R](z: R) = "{" + z.toString() + "}"
          
    }

    
    

    Output:

    {32.0}
    

    Here, the apply function contains an another function x with a value y and applies the function x to y.

  • Example :




    // Scala program of higher order
    // function
      
    // Creating object
    object GfG 
    {
        // Main method
        def main(args: Array[String]) 
        {
          
            // Creating a list of numbers
            val num = List(7, 8, 9)
          
            // Defining a method
            def multiplyValue = (y: Int) => y * 3
          
            // Creating a higher order function 
            // that is assigned to the variable
            val result = num.map(y => multiplyValue(y))
          
            // Displays output
            println("Multiplied List is: " + result)
        }
    }

    
    

    Output:

    Multiplied List is: List(21, 24, 27)
    

    Here, map is a function that takes another function i.e, (y => multiplyValue(y)) as a parameter so, it is a higher order function.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads