Site icon Hari Vignesh

Functional Programming in Kotlin - f(4)

Currying Function

This is the fourth part of the functional programming series. If you have missed the previous one, start here.

Series pit stops

In this pit stop, we’ll take a brief look at how we write or represent currying functions in Kotlin.


Mathematical Currying

Since we talked just f(x), here x can be treated as an argument in programming perspective. So our examples before were only for functions with one argument. How do we compose for functions with multiple arguments?

In FP concept, a function cannot have multiple arguments. Shocked? don’t be. If you recall before, a function is just a map from one source set to another. It’s not a map from multiple source sets to target sets. But on the other hand, a product of 2 sets can result in a set. Such a product of sets, resulting a function which is a set, can resemble like a function of multiple arguments.

For example,

f(x, y) = 2x + y

Here the element x is a pair of 2 elements. Let’s take it as 33 and 48.Here the function of 2 sets are called Tuple.

f((33, 48)) = 2x + y = 66 + 48 => 114

Currying a function is nothing but an operation where we apply one function at a time, pass the result to the next and so on, till we reach the result. Let’s take the same example f(x, y) = 2x + y

This can also be written as

f(x)(y) = 2x + y

Let’s consider f(x) as g. Then,

if f(x) = g, then g(y) = 2x + y

When f(x) is applied to be g, then the value x in 2x + y is a constant. It’s not a variable to be substituted with. Hence,

g(y) = 2(33) + y
g(y) = 66 + y = 66 + 48 => 114

In other words, when we apply x for the function f, we get a resulting function g which is applied to y. This is process is called currying a function.

Kotlin Currying

When we represent tuple in Kotlin (also NOT known as function with 2 parameters), we just pass the result to next function. Like

(Int) -> (Int) -> Int

The above represents : A function, that takes an integer as a parameter (Int) and returns a function of (Int) -> Int

It’s time for some examples of currying in Kotlin. Let’s take the above functions and try to represent in Kotlin.

val currying: (Int) -> (Int) -> Int = { x -> { y -> (2 * x) + y } }


// output -> 114

Now, like our previous pitstop – what if we write a generalized function that can curry two functions?

fun r1(x: Boolean): Int = if(x) 1 else 0
fun r2(x: Int): Boolean = if(x == 0) false else true

fun <T, U, V>currying() = {f: (U) -> V -> { g: (T) -> U -> { h: T -> f(g(h)) }}}

val r1of2 = compose<Int, Boolean, Int>()(::r1)(::r2)

println(r1of2(2)) // output -> 1

In short, currying is a process in functional programming in which we can transform a function with multiple arguments into a sequence of nesting functions. It returns a new function that expects the next argument inline. It keeps returning the function until it exhausts the arguments.

Additional References

If you think you have learned something new or if you like this series or you want to boost my morale, please share the post. Thanks a lot for reading this article. Hope you have enjoyed this series!

Exit mobile version