Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composing functions in Java?

I'm writing demo code for an API we've created and I keep running into the same problem where I'm repeating myself, over and over ad nauseum. I am painfully aware that Java is scheduled to have closures added but I don't have access to them now. Here is what is repeated all over the place that I'd like to just box into it's own little corner:

public BarObj Foo(Double..._input){
    try{
        //things that vary per function
        //but everything else...
    } catch(NullException _null){
        m_Logger.error("Null error exception caught in Blah::Foo");

        return null;
    } catch(Exception ex){
        m_Logger.error( ex.getMessage() );

        return null;
    }
}

About the only way I've thought to go around this is by passing a Method into a function which carries with it the try-catch logic and wrapping it all up in another function like so:

public BarObj MyFunc(Double..._input){
    return compose("MyLogic",_input);
}

private BarObj MyLogic(Double..._input) 
    throws Exception{
    //stuff
}

but it looks ugly and carries with it a lot of boilerplate. Is there an easier way to compose functions in Java?

like image 621
wheaties Avatar asked Feb 05 '10 14:02

wheaties


People also ask

What is composition in functional programming?

Composition is the process of composing small units into bigger units that solve bigger problems. Where Input of one function comes from the output of previous one. For example, if we want to build a castle 🏰 we'll have to compose bricks 🧱

What is composing in Javascript?

Composition is about creating small functions and creating bigger and more complete functions with them. Think of a function as a brick, composition is how you would make those bricks work together to build a wall or a house.

What is function identity () in Java?

identity. static <T> Function<T,T> identity() Returns a function that always returns its input argument. Type Parameters: T - the type of the input and output objects to the function Returns: a function that always returns its input argument.


2 Answers

in Java this is very difficult since there is no first class support for functions (unlike clojure or scala and probably other).

However, you can encapsulate the operation in an object:

interface Function<R, T> {

     R call(T... input);
}

then refactor Foo as:

static <R, T> R runFunction(Function<R, T> function, T ... input){
    try{
       return function.call(input);
    } catch(NullPointerException _null){
       m_Logger.error("Null error exception caught in Blah::Foo");
       return null;
    } catch(Exception ex){
       m_Logger.error( ex.getMessage() );
       return null;
    }
}

testcase:

class SumDoubles implements Function<Double, Double> {

    @Override
    public Double call(Double... input) {
        Double sum = 0.0;

        for (Double d : input) {
            sum += d;
        }

        return sum;
    }
}

@Test
public void sum() {
    Double sum = runFunction(new SumDoubles(), 1.0, 2.0, 3.0);
    assertThat(sum, is(6.0));
}
like image 193
dfa Avatar answered Sep 25 '22 17:09

dfa


Try lambdaj. It allows to add a bit of FP to java in a quite light and readable way. In particular in this closure wiki page you can find an example very similar to what you are trying to achieve,

like image 21
Mario Fusco Avatar answered Sep 25 '22 17:09

Mario Fusco