How can I refer a Lambda from inside of it, if, for example, I need to use myLambda
recursively?
myLambda -> {expression}
// ^^^^^^^^^^ how can I refer to myLambda here?
How can I refer a Lambda from inside of it, if, for example, I need to use myLambda
recursively?
myLambda -> {expression}
// ^^^^^^^^^^ how can I refer to myLambda here?
I misunderstood your question. Here's how you call a lambda expression recursively :
import java.util.function.*;
public class Test
{
static Function<Integer, Integer> fib = null;
public static void main (String[] args)
{
fib = n ->
n == 0 ? 0
: n == 1 ? 1
: fib.apply(n - 1) + fib.apply(n - 2);
System.out.println(fib.apply(8));
}
}
This produces the output 21.
I borrowed the example from Jon Skeet and made the changes required to make it work.
You can find another example of a recursive lambda expression here.
fib.apply(8)
will try to call fib.apply(7) + fib.apply(6);
but at that time fib
is still null
right? So why don't you get a NPE? –
Concernment If you mean you want to refer to the lambda expression you're defining within that lambda expression, I don't believe there's any such mechanism. I know of a few cases where it would be useful - recursive definitions, basically - but I don't believe it's supported.
The fact that you can't capture non-final variables in Java makes this even harder. For example:
// This doesn't compile because fib might not be initialized
Function<Integer, Integer> fib = n ->
n == 0 ? 0
: n == 1 ? 1
: fib.apply(n - 1) + fib.apply(n - 2);
And:
// This doesn't compile because fib is non-final
Function<Integer, Integer> fib = null;
fib = n ->
n == 0 ? 0
: n == 1 ? 1
: fib.apply(n - 1) + fib.apply(n - 2);
A Y-combinator would help here, but I don't have the energy to come up with an example in Java right now :(
I misunderstood your question. Here's how you call a lambda expression recursively :
import java.util.function.*;
public class Test
{
static Function<Integer, Integer> fib = null;
public static void main (String[] args)
{
fib = n ->
n == 0 ? 0
: n == 1 ? 1
: fib.apply(n - 1) + fib.apply(n - 2);
System.out.println(fib.apply(8));
}
}
This produces the output 21.
I borrowed the example from Jon Skeet and made the changes required to make it work.
You can find another example of a recursive lambda expression here.
fib.apply(8)
will try to call fib.apply(7) + fib.apply(6);
but at that time fib
is still null
right? So why don't you get a NPE? –
Concernment If you want to define a recursive function, use Java’s canonical way to implement a recursive function: a method:
public static int fib(int n) {
return n==0? 0: n==1? 1: fib(n-1)+fib(n-2);
}
Then, if you need a instance fulfilling a functional interface
you can use a method reference:
Function<Integer, Integer> fib = MyClass::fib;
or
IntUnaryOperator fib0=MyClass::fib;
© 2022 - 2024 — McMap. All rights reserved.
this
"this
doesn't refer class, but instance of class. – Elurdthis
. This is a major simplification over inner classes, whose name-lookup rules are highly complex and error-prone. – Goldsberry