Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I chain functional calls in Java?

I have two similar pieces of Code:

void task1() {
    init();
    while(someCondition) {
      doSomething();
    }
    shutdown();
  }
void task2() {
    while(someCondition) {
      init();
      doSomething();
      shutdown();
    }
  }

I would like to avoid code duplication and I thought this could be done by using a functional approach. I want to put the loop and the init/shutdown call in seperate functions and then chain their calls (not the Java 8 Function interface, more pseudocode):

Function setup(Function f){
    init();
    f();
    shutdown();
}
Function loop(Function f){
    while(someCondition) {
      f();
    }
}

Then I want to chain these like this:

void task1() {
   setup(loop(doSomething));
 }
void task2() {
    loop(setup(doSomething));
  }

I thought of compose/andThen in Java's Function interface but they are not suitable because they only hand on the return value of one function to the next one. Does anyone have an idea how to do this with Java 8 or with a different approach?

like image 375
Max Zofal Avatar asked Jan 07 '20 13:01

Max Zofal


People also ask

Which creates a chain class by calling?

The calling of the constructor can be done in two ways: By using this() keyword: It is used when we want to call the current class constructor within the same class. By using super() keyword: It is used when we want to call the superclass constructor from the base class.

How do you use multiple functions in Java?

Java permits defining multiple methods with the same name, as long as they can be distinguished based on their parameters: That is, any two methods of the same name must have a different number of parameters — or, if they have the same number of parameters, one of the methods must have a parameter whose type is ...

Can you chain methods?

Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.


1 Answers

You can indeed do this. You need Runnable, not Function, since your methods accept no parameters and return no value. If your methods have a different signature though, you need to use another type.

public static void init() { ... }
public static void doSomething() { ... }
public static void shutdown() { ... }

public static Runnable setup(Runnable r) {
    return () -> {
        init();
        r.run();
        shutdown();
    };
}

public static Runnable loop(Runnable r) {
    return () -> {
        while (someCondition) {
            r.run();
        }
    };
}

// I used "Main" here because this in a class called Main. Replace "Main" with the name of your class
public static void task1() {
    setup(loop(Main::doSomething)).run();
}

public static void task2() {
    loop(setup(Main::doSomething)).run();
}

It should also be noted that although to a functional programmer's eyes, the first code might look "duplicated", to Java programmers, the first code is very fine as it is. Rewriting it this way might be more confusing to people who are not used to the functional style.

like image 136
Sweeper Avatar answered Oct 03 '22 05:10

Sweeper