1

Since we can adopt some functional programming concept in Java programming language is it also possible to write a function which returns an other function? Since Higher order functions do that. Just like JavaScript ?

Here is a JavaScript code that returns a function.

function magic() {
  return function calc(x) { return x * 42; };
}

var answer = magic();
answer(1337); // 56154
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Kidus Tekeste
  • 651
  • 2
  • 10
  • 28
  • Does this answer your question? [Function within a function in Java](https://stackoverflow.com/questions/7097275/function-within-a-function-in-java) – Dren Feb 19 '21 at 23:00
  • @Dren No Dren. It's a different question. I wanted to know if it is possible to return a function from inside another function in Java. Not defining a function inside a function, which we CAN NOT. – Kidus Tekeste Feb 19 '21 at 23:03
  • When you say "function" do you mean methods or instances of functional interfaces? – sepp2k Feb 19 '21 at 23:06
  • 2
    You can’t technically return a function, but you can return the instances of classes representing functions, in your case `UnaryFunction`. It’s ugly though. – Abhijit Sarkar Feb 19 '21 at 23:09
  • When I say a "function" I mean a "method" @sepp2k – Kidus Tekeste Feb 19 '21 at 23:11

3 Answers3

4

The closest Java equivalent is this:

public static void main(String[] args) {
    UnaryOperator<Integer> answer = magic();
    System.out.println(magic().apply(1337));  // 56154
    System.out.println(answer.apply(1337));   // 56154
}

static UnaryOperator<Integer> magic() {
    return x -> x * 42;
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
1

Yes you can

Function<String, Integer> getLengthFunction() {
    return String::length;
}

And you can take this further. E.g. implement currying:

static <T, U, R> Function<T, Function<U, R>> curry(BiFunction<T, U, R> f) {
    return t -> u -> f.apply(t, u);
}
michid
  • 10,536
  • 3
  • 32
  • 59
  • 1
    This is beautiful, ,I have never seen such implementation before and yes its a function return. Thank you. But Nikolas' answer was according to my question's example and would make it clear for next reader. – Kidus Tekeste Feb 20 '21 at 04:21
1

Here's another example I wrote a while back. It returns a conversion function base on supplied arguments.

// generate a Celsius to Fahrenheit Converter.
DoubleUnaryOperator cToF = conversionGen(9 / 5., 32);

// Fahrenheit to Celsius
DoubleUnaryOperator fToC =
        conversionGen(5 / 9., (-32 * 5 / 9.));

// kilometers to miles
DoubleUnaryOperator kiloToMiles = conversionGen(.6, 0);

// and pounds to Kilograms.
DoubleUnaryOperator lbsToKgms = conversionGen(1 / 2.2, 0);

Now all you have to do is call them with the single argument.

double farTemp = cToF.applyAsDouble(100);
double celTemp = fToC.applyAsDouble(212);
System.out.println(farTemp);
System.out.println(celTemp);
System.out.println(kiloToMiles.applyAsDouble(1500));
System.out.println(lbsToKgms.applyAsDouble(4.4));

Prints

212.0
100.0
900.0
2.0     

The generator.

public static DoubleUnaryOperator conversionGen(double factor,
            double base) {
        return unit -> unit * factor + base;
}
WJS
  • 36,363
  • 4
  • 24
  • 39
  • Yes this is correct and Thank you so much for the well explained answer. Am gonna have to choose Nikolas' answer because it was according to my question's explanation and was quick. – Kidus Tekeste Feb 20 '21 at 04:19
  • 1
    @KidusTekeste No explanation is necessary. The important this is you got your answer. – WJS Feb 20 '21 at 04:21