A Short Note – An Overview Of Functions Currying In Kotlin

Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (An Overview Of Functions Currying In Kotlin).

In this note series, We will understand about Functions Currying in Kotlin. Currying is a common technique in functional programming. It allows transforming a given function that takes multiple arguments into a sequence of functions, each having a single argument.

In this short note series, we are going to implement an automatic currying mechanism that could be applied to any function taking three parameters.

So Let’s begin.

Overview

We can understand Currying as :

  • A common technique in functional programming.
  • Transforming a given function that takes multiple arguments into a sequence of functions, each having a single argument.
  • Each of the resulting functions handles one argument of the original (uncurried) function and returns another function. 

To understand the concept of functions currying, let’s consider the following example of a function handling three parameters:

fun foo(a: A, b: B, c: C): D 

Its curried form would like this :

fun carriedFoo(a: A): (B) -> (C) -> D 

In other words, the curried form of the foo function would take a single argument of the A type and return another function of the following type: (B) -> (C) -> D. The returned function is responsible for handling the second argument of the original function and returns another function, which takes the third argument and returns a value of type D. 

How To Implement it ?

In this section, we are going to implement the curried() extension function for the generic functional type declared as follows:  ((P1, P2, P3). The curried() function is going to return a chain of single-argument functions and will be applicable to any function which takes three arguments.

Here, we can implement Functions Currying with the below steps :

Step 1 – Declare a header of the curried() function :

fun<P1, P2, P3, R>((P1, P2, P3) -> R).curried(): (P1) -> (P2) -> (P3) -> R

Step 2 – Implement the curried() function body :

fun<P1, P2, P3, R>((P1, P2, P3) -> R).curried(): (P1) -> (P2) -> (P3) -> R =
{ p1: P1 ->
 { p2: P2 ->
  { p3: P3 ->
    this(p1, p2, p3)
  }
 }
}

How To Works ?

Let’s explore how to use the curried() function in action. In the following example, we are going to call curried() on the following function instance which is responsible for computing a sum of three integers:

fun sum(a: Int, b: Int, c: Int): Int = a + b + c

In order to obtain a curried form of the sum() function, we have to invoke the curried() function on its reference:

::sum.curried()

Then we can invoke the curried sum function in the following way:

val result: Int = ::sum.curried()(1)(2)(3)

Here, the result variable is going to be assigned an integer value equal to 6.

In order to invoke the curried() extension function, we access the sum() function reference using the :: modifier. Then we invoke the next functions from the function sequence returned by the curried function one by one. 

The preceding code could be written in an equivalent more verbose form with explicit types declarations:

val sum3: (a: Int) -> (b: Int) -> (c: Int) -> Int = ::sum.curried()
val sum2: (b: Int) -> (c: Int) -> Int = sum3(1)
val sum1: (c: Int) -> Int = sum2(2)
val result: Int = sum1(3)

Under the hood, the currying mechanism implementation is just returning functions nested inside each other. Every time the specific function is invoked, it returns another function with the arity reduced by one.

Conclusion

In this note series, we understood about Functions Currying in Kotlin. Currying is useful whenever we can’t provide the full number of required arguments to the function in the current scope. We can apply only the available ones to the function and return the transformed function.

Thanks for reading! I hope you enjoyed and learned about Functions Currying in Kotlin. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow official website and tutorials of Android as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

Loading

Summary
A Short Note – An Overview Of Functions Currying In Kotlin
Article Name
A Short Note – An Overview Of Functions Currying In Kotlin
Description
This note series explains about Functions Currying in Kotlin that transforms a given function and takes multiple arguments into a sequence of functions.
Author

2 thoughts on “A Short Note – An Overview Of Functions Currying In Kotlin”

  1. This web site is really a walk-through for all of the info you wanted about this and didn’t know who to ask. Glimpse here, and you’ll definitely discover it.

    Reply

Leave a Comment