6

HIGHER ORDER FUNCTIONS IN SCALA

 4 years ago
source link: https://blog.knoldus.com/higher-order-functions-in-scala/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

HIGHER ORDER FUNCTIONS IN SCALA

Reading Time: 2 minutes

In this blog, I’m going to explain higher-order functions.

A higher order function takes other function as a parameter or return a function as a result.

This is possible because functions are first-class value in scala. What does that mean?

It means that functions can be passed as arguments to other functions and functions can return other function.

The map function is a classic example of a higher order function.

xxxxxxxxxx
val list = List(1,2,3)

Let’s define a function that doubles each value that is given to it.

xxxxxxxxxx
def doubleValue = (x: Int) => x * x
doubleValue: Int => Int
val doubledList = list.map(x => doubleValue(x))

will call List(1,2,3).map(x => doubleValue(x)) and gives us a list with values (1, 4, 9).  Function map will called on each element of the list that will be passed to doubleValue.

We can also give it an anonymous function.

xxxxxxxxxx
List(1,2,3) map (x => x + 1)

map is a function which takes a function(x => x+1) and a list as its arguments and applies given function to all the elements of the list.

Let’s create our own higher order function: Example 1:
xxxxxxxxxx
def addition(f: (Int, Int) => Int,a: Int, b:Int): Int = f(a,b)

addition takes higher order function as input which inturn takes two integers as input and returns an integer.

xxxxxxxxxx
val squareSum = (x: Int, y: Int) => (x*x + y*y)
val cubeSum = (x: Int, y: Int) => (x*x*x + y*y*y)
val intSum = (x: Int, y: Int) => (x + y)
val squaredSum = addition(squareSum, 1, 2)
val cubedSum = addition(cubeSum, 1, 2)
val normalSum = addition(intSum, 1, 2)

addition(squareSum, 1, 2) will call squareSum(1,2)
addition(cubeSum, 1, 2) will call cubeSum(1,2)
addition(intSum, 1, 2) will call intSum(1,2)

Example 2:
xxxxxxxxxx
def applyPatternToText(text:String,f:String => String): String = {
    f(text)
}

applyPatternToText functions takes another function as a parameter.

xxxxxxxxxx
def appendTag(data:String): String => String = {
      _ : String => s"$data"
}

appendTag returns a function itself.

xxxxxxxxxx
val message = "scala"
println( applyPatternToText(message, appendTag(message)))

will call applyPatternToText(“scala”,appendTag(“scala”))

f:String => String will be replaced by appendTag(“scala”)

appendTag(“scala”) will print scala

Example 3:

Let’s take another example to find sum of a list and product of a list using higher order function:

xxxxxxxxxx
  def operateList(list: List[Int], f: (Int, Int) => Int, operation: String): Int = {
    def inner(list: List[Int], result: Int): Int = {
      list match {
        case head :: tail => inner(tail, f(head, result))
        case Nil => result
      }
    }
    operation.toLowerCase match {
      case "product" => inner(list, 1)
      case "sum" => inner(list, 0)
    }
  }

operateList(List(1,2,3),(a, b) => a + b, “sum”) will call inner(List(1,2,3),0).

After match condition, next call will be inner(List(2,3),f(1,0)) which will give inner(List(2,3),1).

Next call would be inner(List(3),f(2,1)) which will give inner(List(3),3)
Next call would be inner(Nil,f(3,3)) which will give inner(Nil,6)

The final result would be 6.

Thanks for reading!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK