Scala Language Higher Order Function High Order Functions(Function as Parameter)

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

A higher-order function, as opposed to a first-order function, can have one of three forms:

  • One or more of its parameters is a function, and it returns some value.

  • It returns a function, but none of its parameters is a function.

  • Both of the above: One or more of its parameters is a function, and it returns a function.

    object HOF {
        def main(args: Array[String]) {
        val list = List(("Srini","E"),("Subash","R"),("Ranjith","RK"),("Vicky","s"),("Sudhar","s"))
        //HOF
         val fullNameList= list.map(n => getFullName(n._1, n._2))
    
         }
    
        def getFullName(firstName: String, lastName: String): String = firstName + "." + lastName
        }
    

Here the map function takes a getFullName(n._1,n._2) function as a parameter. This is called HOF (Higher order function).



Got any Scala Language Question?