Higher Order Functions in Scala

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 :



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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() + "}"
          
    }

    chevron_right

    
    

    Output:

    {32.0}
    

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

  • Example :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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)
        }
    }

    chevron_right

    
    

    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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.