Posts

Showing posts with the label lambda

Why don't instance fields need to be final or effectively final to be used in lambda expressions?

28 8 I'm practicing lambda expressions in Java. I know local variables need to be final or effectively final according to the Oracle documentation for Java SE 16 Lambda Body : Any local variable, formal parameter, or exception parameter used but not declared in a lambda expression must either be final or effectively final (§4.12.4), as specified in §6.5.6.1. It doesn't say why though. Searching I found this similar question Why do variables in lambdas have to be final or effectively final?, where StackOverflow user "snr" responded with the next quote: Local variables in Java have until now been immune to race conditions and visibility problems because they are accessible only to the thread executing the method in which they are declared. But a la...

Java method can't be applied with Lambda expression

I've watched and read https://caveofprogramming.com/java/whats-new-in-java-8-lambda-expressions.html and I follow the same pattern I did for runner object which works fine. Runner runner = new Runner(); runner.run(() -> System.out.println("Print from Lambda expression")); Then, I try to create a simple interface and class to apply what I learned. I just want to replace the anonymous class with a lambda expression. My understanding is a lambda expression is a shorter code for the anonymous class and improve readability. So, I tried to initiate another instance called eucalyptus1 and try to @Override the grow() method, but my IDE error message said: grow() in com.smith.Eucalyptus cannot be applied to (lambda expression) Could anyone point me out what I misunderstand here? The code is below: // a simple interface interface Plant { public void grow(); } // apply interface to class class Eucalyptus implements Plant { @Override public void grow() { Syst...

How to iterate over lambda functions in Java

I was able to do it in Python and my Python code is: signs = {"+" : lambda a, b : a + b, "-" : lambda a, b : a - b} a = 5 b = 3 for i in signs.keys(): print(signs[i](a,b)) And the output is: 8 2 How do I do this same thing in Java through HashMap? You can use BinaryOperator<Integer> in this case like so : BinaryOperator<Integer> add = (a, b) -> a + b;//lambda a, b : a + b BinaryOperator<Integer> sub = (a, b) -> a - b;//lambda a, b : a - b // Then create a new Map which take the sign and the corresponding BinaryOperator // equivalent to signs = {"+" : lambda a, b : a + b, "-" : lambda a, b : a - b} Map<String, BinaryOperator<Integer>> signs = Map.of("+", add, "-", sub); int a = 5; // a = 5 int b = 3; // b = 3 // Loop over the sings map and apply the operation signs.values().forEach(v -> System.out.println(v.apply(a, b))); Outputs 8 2 Note for Map.of("+", add, "-...

Meaning of lambda () -> { } in Java

I am looking at the following Stack Overflow answer: How to change Spring's @Scheduled fixedDelay at runtime And in the code there is the following line: schedulerFuture = taskScheduler.schedule(() -> { }, this); I would like to know what the lambda () -> {} means in that code. I need to write it without using lambdas. Its a Runnable with an empty run definition. The anonymous class representation of this would be: new Runnable() { @Override public void run() { // could have done something here } } Lamda expression is an anonymous function that allows you to pass methods as arguments or simply, a mechanism that helps you remove a lot of boilerplate code. They have no access modifier(private, public or protected), no return type declaration and no name. Lets take a look at this example. (int a, int b) -> {return a > b} In your case, you can do something like below: schedulerFuture = taskScheduler.schedule(new Runnable() { @Override ...

Lambda returning itself: is this legal?

Consider this fairly useless program: #include <iostream> int main(int argc, char* argv[]) { int a = 5; auto it = [&](auto self) { return [&](auto b) { std::cout << (a + b) << std::endl; return self(self); }; }; it(it)(4)(6)(42)(77)(999); } Basically we are trying to make a lambda that returns itself. MSVC compiles the program, and it runs gcc compiles the program, and it segfaults clang rejects the program with a message: error: function 'operator()<(lambda at lam.cpp:6:13)>' with deduced return type cannot be used before it is defined Which compiler is right? Is there a static constraint violation, UB, or neither? Update this slight modification is accepted by clang: auto it = [&](auto& self, auto b) { std::cout << (a + b) << std::endl; return [&](auto p) { return self(self,p); }; }; it(it,4)(6)(42)(77)(999); Update 2: I understand how to write a functor that returns itself, or ho...