Scala Higher Order Function

In scala, when a function accepts another function as an argument or returns a function as an output, is called Higher order function. Let’s understand this with examples.

Example1

Map is the good example for understanding higher order function. To demonstrate how this accepts a function as argument, we will create a function which will multiply the same number twice and then we will pass that function into map.

object multiply {
  def main(args: Array[String]): Unit = {
    def mulNum(a:Int) = a * a  //Function to multiply same number
    val lst = List.range(1,5)
    print(lst.map(mulNum))
  }
}
Output: List(1, 4, 9, 16)

We can also pass the anonymous function to perform the same task as above. For that, we can create an anonymous function as given below.

Anonymous function: x => x * x

It will give the same output.

object multiply {
  def main(args: Array[String]): Unit = {
    val lst = List.range(1,5)
    print(lst.map(x=>x*x))
  }
}
Output: List(1, 4, 9, 16)

Example2

Now, Let’s create a user defined higher order function which accepts a function as an arguments and returns nothing.

We will pass the below function as an argument, which itself accepts a String parameter and returns string parameter.

f:(String) => String

Next, define the function that matches the signature with above function which accept string parameter and return string parameter.

val myname = (name: String) => s”$name”

Now, Create a higher order function which takes 2 arguments and print the message as output of the function.

def PrintMsg(prefix: String) = (name:String ) => {
prefix + " " + name
}

We can call the function as given below.

val name="Shekhar"
printMsg(myname,name)

Now, let’s combine the full code and see the output.

object PrintName {
  def main(args: Array[String]): Unit = {
    val myname = (name: String)  => s"$name"
    def printMsg(f:(String) => String,msg:String){
      print(f(msg))
    }

    val name="Shekhar"
    printMsg(myname,name)

  }
}
Output: Shekhar

Example3

Let’s create a function that returns an anonymous function, assign that to a new function and then call that new function.

Define an anonymous function that takes a String as an argument and returns String.

(name:String)=>{ prefix + " " + name}

Let’s create a function that returns the above anonymous function from its body.

def PrintMsg(prefix: String) = (name:String ) => {
  prefix + " " + name
}

Now, We will call that function and assign it to a new function. New function will make another call with desired message to be printed.

val myNewFunc = PrintMsg("Hello")
print(myNewFunc("Thomas"))

Let’s combine the whole code and see the output.

object PrintName {
  def main(args: Array[String]): Unit = {
    def PrintMsg(prefix: String) = (name:String ) => {
      prefix + " " + name
    }

    val myNewFunc = PrintMsg("Hello")
    print(myNewFunc("Thomas"))
  }
}
Output: Hello Thomas

Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...