Why Currying functions is important in Java

Why Currying functions is important in Java

Currying functions is the ability to pass some of the method parameters now, and the rest of those parameters later, so if a function has 2 parameter, we can invoke it with the first parameter, which will return another function waiting for the 2nd parameter ... when we pass the second parameter to this new function, the whole function is executed

This concept is very common in functional programming, but in Java, it is not ... so we have to make some work around stuff.

The following example is for some operations in Java, using Rx-Java, Guava and Retro-lambda, which helps in cutting too many lines of code using "method reference"

public void withoutFunctionsCurrying(){
    Observable.fromArray("1","2","3")
            .doOnNext(this::printNumbers)
            .map(Integer::valueOf)
            .map(this::multiplyByTwo)
            .map(this::formatToDoubleValue)
            .forEach(this::printFinalNumbers);
}

private void printNumbers(String number){
    Log.d("NUMBERS",number);
}

private int multiplyByTwo(int value){
    return IntMath.checkedMultiply(2,value);
}

private String formatToDoubleValue(int value){
    return Joiner.on(": ").join("double value",value);
}

private void printFinalNumbers(String number){
    Log.d("FINAL NUMBERS",number);
}

But the real issue begins when ever we need to pass a function that takes 2 parameters, so for example, in the first doOnNext(), we cannot pass Log::d since this method takes 2 parameters, and doOnNext() is waiting for a function that takes one parameter, so we have to create printNumbers() method just to add the first parameter

private void printNumbers(String number){
    Log.d("NUMBERS",number);
}

then we can pass reference to this method which takes one String parameter.

This approach is nice, but it results in declaring 4 extra methods that will be used only for this method.

On the other hand, if we used currying the code will end up like this :

public void withFunctionsCurrying() {
    Observable.fromArray("1", "2", "3")
            .doOnNext(Curry.toConsumer(Log::d, "NUMBERS"))
            .map(Integer::valueOf)
            .map(Curry.toFunction(IntMath::checkedMultiply, 2))
            .map(Curry.toFunction(Joiner.on(": ")::join, "double value"))
            .forEach(Curry.toConsumer(Log::d, "FINAL NUMBERS"));
}

* I used J-Curry library for Currying functions, but you can use what ever technique you prefer

so now when ever there is a function that takes 2 parameters, we can curry it and pass to it the first parameter, it will return another function that is expecting only one last parameter (which will be emitted by Rx-Java operator)

I hope that Kotlin should provide such powerful feature.


To view or add a comment, sign in

More articles by Ahmed Adel Ismail

  • Sharing data across multiple Mobile Squads - with examples

    Earlier I shared an article suggesting a solution to a common problem with teams following the "Spotify Model", which…

  • SDD - Squad Driven Design

    Working in multiple big teams I've found that we are always trying to apply our known best practices in software, but…

    4 Comments
  • Easier Testing with MVVM, MVI, MVP and Kotlin Multiplatform

    Before we start, this article requires basic knowledge about the following topics : Clean Architecture Unit Testing…

    9 Comments
  • Android - A Cleaner Clean Architecture

    It has been a while now since Clean Architecture was out, and even many of us started embracing hexagonal (ports and…

    9 Comments
  • Beyond Functional Programming

    In the Android industry, lately functional programming was the all new stuff to learn, RxJava, Kotlin, and the whole…

    7 Comments
  • Dependency Injection in Clean Architecture

    After Google's Opinionated Guide to Dependency Injection Video, Google made a clear statement that they want developers…

    18 Comments
  • Meta Programming in Android

    Year after year we are getting rid of the boilerplate code that we need to write for small and simple tasks in Android,…

    2 Comments
  • MVI Pattern For Android In 4 Steps

    Lately I wrote an article about MVI pattern, but as we are facing new problems every day and face more use-cases, we…

    7 Comments
  • Agile - Moving Fast

    We always here about Agile, and think about which methodology do we use, what practices do we have, team velocity…

    1 Comment
  • Kotlin Unit Testing with Mockito

    I've always believed that, if the code is designed to be tested, we wont need any testing framework or library ..

    17 Comments

Others also viewed

Explore content categories