Currying in Kotlin

amol pawar
softAai Blogs
Published in
11 min readMay 6, 2023

--

Currying is a programming technique that involves transforming a function that takes multiple arguments into a series of functions that take a single argument. In other words, it’s a way of breaking down a complex function into smaller, simpler functions that can be composed together to achieve the same result. In this blog post, we will explore the concept of currying in Kotlin and how it can be used to write more concise and expressive code.

What is Currying?

Currying is named after Haskell Curry, a mathematician who introduced the concept in the 20th century. At its core, currying is a way of transforming a function that takes multiple arguments into a series of functions that each take a single argument. For example, consider the following function:

fun add(a: Int, b: Int): Int {
return a + b
}

This function takes two arguments, a and b, and returns their sum. With currying, we can transform this function into two nested functions that each take a single argument:

fun addCurried(a: Int): (Int) -> Int {
return fun(b: Int): Int {
return a + b
}
}

Now, instead of calling add(a, b), we can call addCurried(a)(b) to achieve the same result. The addCurried function takes a single argument a and returns a new function that takes a single argument b and…

--

--