What is the Java equivalent of this Ruby lambda?

Asked

Viewed 85 times

5

In his text on Inversion of Control, Fowler uses as an example an excerpt of code in Ruby that promotes the said principle to "invoke a bind method over the text input field that passes an event name and a Lambda as argument. Whenever the text input box detects the event, it calls the code in the enclosure. Using closures like this 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 believe he refers for example to the following line:

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

I don’t know Ruby. I wonder if the introduction of Ambdas in Java 8 made it possible to do something similar and how it would work in his case. I would like if possible the explanation to illustrate why it was not possible to do this in previous versions of Java, for a better understanding of the problem.

1 answer

7


Closures

The following is not lambda, but a block or closure. It is quite common to see blocks in high-order functions acting as first class functions, not only in Ruby but also in most languages. An example in Ruby:

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

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

Other in Javascript:

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

In a simple way, a closure is a function that can be stored in a variable and can access other local variables to its scope.

If it weren’t for the closures, Javascript would not work.

Closures vs Lambdas

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

Closures have a big difference for functions Amble because closures envelop their context, thus giving access to the variables of their lexical scope. And that’s why the closures are also called lexical closures.

Java

I previously commented that closures and Amblas were a form on paper, but they vary from implementation to implementation, so... in Java there are closures, but these are identical to the functions Amble.

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 Java the expressions lambda are encapsulated in the local scope, as well as the closures, conceptually.

Since Java 8, there are lambda expressions. Behold:

StateOwner stateOwner = new StateOwner();

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

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

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

Previous versions of Java

The previous versions of Java allowed to do this, but in a slightly different way and that is not lambda, because 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")
    }
});

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.