October 2, 2025

Scala – Yet More Functions!

In this lesson, we would complete our discussion functions. You will learn the following

  1. Anonymous Functions
  2. Partially Applied Functions
  3. Higher-Order Functions (HOD)
  4. Currying Functions

 

1. Anonymous Functions

In Scala, anonymous functions are also called function literals or unnamed functions. At runtime, they are instantiated into objects called function values.

Functions in Scala can be expressed in function literal syntax as shown below

(x: Int) = x + 1

This means that the function can be represented by its value when evaluated at runtime.

The code below creates a first-class function which means that a function is assigned to a variable just like values are assigned to variables.

var dec = (x:Int) => x-1

The variable dec in the above code is now a function and can be called normally.

We can also define an anonymous function using multiple parameters, for example:

var addUp = (a: Int, b: Int) => a + b

This can also be called in the usual way.

2. Partially Applied Functions

When a function is called, we can say the function is applied to the arguments. The function is fully applied if all the function arguments are passed. However, if only some of the arguments are passed, then we have a partially applied function.

Therefore, in partially applied function, we pass only some of the arguments of the function.

Assuming we have a program that displays my daily activities. This program is shown below and works just fine.

// Program to demostrate Partially Applied Functions
object PartiallyAppliedFunctions {

   def show(date: Date, name: String,  activity: String): Unit ={
     println(name + " - " + date + " - " + activity)
   }

  def main(args: Array[String]) {
    show(new Date, "Kidson The Genius", "Woke Up")

    Thread.sleep(1000)
    show(new Date, "Kidson The Genius", "Read the text")

    Thread.sleep(1000)
    show(new Date, "Kidson The Genius", "Made a video")
  }
}

The program executes and displays the output below:

Kindson The Genius - Fri Jan 21 09:02:42 CET 2022 - Woke Up
Kindson The Genius - Fri Jan 21 09:02:43 CET 2022 - Read the text
Kindson The Genius - Fri Jan 21 09:02:44 CET 2022 - Made a video

If you look at the output, you will see that the name repeats. What we want to do is to partially apply this function with the date and activity since that is what changes when the function is called. To achieve this, we have to create a new first-class function with the name parameter bound, while the other two parameters are unbound. This first-class function is created from the existing function like so:

val partialShow = show("Kindson The Genius", _ : Date, _ : String)

So now we have a new function, partialShow() that we can partially apply by giving it only two parameters. Please watch the video for a demo

 

3. Higher-Order Functions (HOD)

HODs are functions that take other functions as parameters or whose output is another function. The most popular higher order function is the map() function. This takes a function as parameter and also a value and applies the function to the value.

In the example below, the function map() takes another function addTen() as parameter.

// Program to demostrate Higher Order Functions
object HigherOrderFunctions {

  def main(args: Array[String]): Unit ={
    val scores = Seq(89, 65, 77)
    val addTen = (x: Int) => x + 10
    val newScores = scores.map(addTen)

    println(scores)
    println(newScores)
  }
  
}

 

 

4. Currying Functions

Currying functions are functions that takes multiple parameters into a chain while taking each parameter as a single parameter. The syntax for currying function is given below

def strJoin(str1: String) = (str2: String) => str1 + str2

 

Then you can call the currying function like so:

strJoin("kany")("muno")

 

The program below is an example of how currying function works.

object CurryingFunctionDemo {
   def main(args: Array[String]) {
      val s1:String = "Hello, "
      val s2:String = "World!"
      
      println( "s1 + s2 = " +  strcat(s1)(s2) )
   }

   def strcat(str1: String)(str2: String) = {
      str1 + str2
   }
}

 

Leave a Reply