What is the Java equivalent of this lambda in Ruby?

5

In your Inversion of Control text, Fowler uses as an example a Ruby code snippet that promotes that principle to invoke a bind method on the input field of text that passes an event name and a lambda as argument.Whenever the text input box detects the event, it calls the code in the cloister. is very convenient, but many languages do not support them.

require 'tk'
root = TkRoot.new()
name_label = TkLabel.new() {text "What is Your Name?"}
name_label.pack
name = TkEntry.new(root).pack
name.bind("FocusOut") {process_name(name)}
quest_label = TkLabel.new() {text "What is Your Quest?"}
quest_label.pack
quest = TkEntry.new(root).pack
quest.bind("FocusOut") {process_quest(quest)}
Tk.mainloop()

I think it refers to the following line for example:

name.bind("FocusOut") {process_name(name)}

I do not know Ruby. I was wondering if introducing lambdas in Java 8 allowed doing something similar and how it would work in his case. I would if possible explain why it was not possible to do this in earlier versions of Java, for a better understanding of the problem.

    
asked by anonymous 12.06.2018 / 02:52

1 answer

7

Closures

The following is not lambda, but a block or closure . It's quite common to see blocks in high order functions acting as #

[1, 2, 3].map { |item| item + 1 }
# => [2, 3, 4]

In the example, Array#map is a high-order function, and the / block function, passed as a parameter, acts as a first-class function.

Other in JavaScript:

[1, 2, 3].map(function(item) {
    return item + 1;
});
// => [2, 3, 4]

Simply put, a closure is a function that can be stored in a variable and can access other local variables in the scope of the variable.

If it were not closures, JavaScript would not work .

Closures vs Lambdas

This is a long discussion of differs and implementation for implementation. A lambda function is an anonymous, unnamed function. In many languages, a lambda is internally converted into a normal function declaration.

Closures have a large difference for lambdas functions as closures are enveloped in their context, thereby giving access to variables in their lexical scope . And that's why closures are also called lexical closures.

Java

Previously I commented that closures and lambdas were in a way on paper, but they vary from implementation to implementation, so ... in Java there are closures , but these are identical to lambdas / em>.

  

A closure is a lambda expression paired with an environment that binds   each of its free variables to a value. In Java, lambda expressions   will be implemented by means of closures, so the two terms have come   to be used interchangeably in the community. Source.

This means that in lambda expressions lambda are encapsulated in the local scope, as well as closures , conceptually.

Since Java 8, there are lambda expressions . See:

StateOwner stateOwner = new StateOwner();

stateOwner.addStateListener(
    (oldState, newState) -> System.out.println("State changed")
);

This is the lambda (which also acts as closure) in Java.

(oldState, newState) -> System.out.println("State changed")

Previous versions of Java

Previous Java versions allowed this to be done, but in a slightly different way and not lambda, as they were not anonymous functions.

StateOwner stateOwner = new StateOwner();

stateOwner.addStateListener(new StateChangeListener() {

    public void onStateChange(State oldState, State newState) {
        System.out.println("State changed")
    }
});

Note that the onStateChange function has a name and is not a lambda. But it was possible to pass one function as a parameter to another.

Another obstacle was that in Java 7 or lower, to pass a function as a parameter of another, the signature of the method that receives should be an interface, thus respecting the types. In Java 8 you can use Function<TipoRetorno> .

12.06.2018 / 04:28