9

Following obviously works, but I do not like to wrap items in Tuple,

    ImmutableMap<String, Function<Tuple2<Double>, Double>> op = new //
    ImmutableMap.Builder<String, Function<Tuple2<Double>, Double>>()
            .put("+", new Function<Tuple2<Double>, Double>() {
                @Override public Double apply(Tuple2<Double> data) {
                    return data.Val_1 + data.Val_2;
                }
            }).build();
    System.out.println(op.get("+").apply(new Tuple2<Double>(3d, 4d)));

I want to write something like:

    ImmutableMap<String, Function<Double[], Double>> op = new //
    ImmutableMap.Builder<String, Function<Double[], Double>>()
            .put("+", new Function<Double[], Double>() {
                @Override
                public Double apply(Double... data) {
                    return data[0] + data[1];
                }
            }).build();
    System.out.println(op.get("+").apply(3d, 4d));

Help would be most useful, ty.

Edit: Problem solved, started using:

public interface T2Function<T> {
    T apply(T Val_1, T Val_2);
}
Margus
  • 19,694
  • 14
  • 55
  • 103
  • You could do this by returning another `Function` from your `Function.apply`, and then applying that to the second argument. `Function.apply(a).apply(b)`. The "inner" function would use `a` directly, and receive `b` as a parameter. This could be extended for `n` parameters. That would mimic Haskell's treatment of functions, but I'm not sure what you would gain from it in Javaland. – jpaugh Dec 11 '15 at 15:03

2 Answers2

15

I think you'd be better off using an interface of your own, something like this:

public interface Operation {
  double apply(double a, double b);
}

Guava's Function is a single argument function and not really appropriate for anything multi-argument.

Another thing I've experimented with is a ReduceFunction<F, T> that happens to be usable for such a thing. It's for use with the reduce or fold operation and looks something like:

public interface ReduceFunction<F, T> {
  T apply(T a, F b); // I can't decide on good names for the parameters =(
}

This lets you do things like

List<Double> doubles = ...
Double sum = reduce(doubles, MathOps.add(), 0.0);

where MathOps.add() is a ReduceFunction<Double, Double> that does the obvious thing.

ColinD
  • 108,630
  • 30
  • 201
  • 202
  • @CollinD: What is reduce and fold ? I don't get it. – Emil Oct 28 '10 at 05:16
  • 4
    @Emil: They are functional programming constructs, aka higher-order functions. A `reduce` will generally reduce a list into a single value by combining elements through a supplied function. A `fold` is similar to a reduce operation, except that it can be seeded with an initial value. The function supplied to the `fold` will then combine the list elements and the seeded value. – gpampara Oct 28 '10 at 06:26
  • @gpampara: Is such functional programming done using an api ? – Emil Oct 28 '10 at 06:52
  • 3
    @Emil: Within the Java world, yes. Guava adds some of the foundations for these, making it a very good library to lever. Remember that there are Functional Programming languages that support this out the box - Scala, Lisp, Haskell etc. The addition of closures to Java will make higher-order functions in Java simpler, but looks like we're gonna have to wait for JDK 8 for those. – gpampara Oct 28 '10 at 07:12
  • If you don't have lambdas - i.e., you're stuck on Java 7 as I am for the current project :( - I would go for an expressively named interface, because that's more readable then `ReduceFunction` – Stefan Haberl Apr 15 '16 at 08:35
3

It looks like you're after an equivalent to c#'s Func: Specialised in your case with the args and return value of the same type.

There's two other questions with good answers about this..

As some other answers hint at, you may be stuck in the middle between two paradigms here (OO and functional), something with which language design is arguably catching up quicker than good practice is. If you want to continue down that murky water, you could try functionaljava.

See Mixing object-oriented and functional programming for more interesting discussion.

Community
  • 1
  • 1
Sam
  • 581
  • 4
  • 17