File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes clojure functors vs normal methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "clojure functors vs normal methods" Watch "clojure functors vs normal methods" New topic
Author

clojure functors vs normal methods

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Can anyone point the pros/cons of using a clojure (from apache.collections) instead of a normal java-method?

For example, why should I use something like this:

public static class testClojure(){

Closure c = new Closure( ) {

public void execute(Object o) {
...
}
}

and invoke it via c.execute(instance);

when I can do encapsulate execute-method in a separate method without wrapping a closure:

public static class noClojure(){

..
this.execute(instance);

}


public void execute(Object o) {

}


I do not get the point when to use one and the other.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

You use closures when you need to 'pass' a method to another object.

Threads are a good example. When you start a new thread, that thread will want to know what to execute. In other languages, like C, we can simply pass our function directly to the thread. We say, "here is the address of the function you should execute". Sadly, you can't do this in Java. In Java, you can give a thread something to execute by making a closure and passing it to the thread. In Java, we call these closures "Anonymous classes". Here is an example:

Just stick with normal methods. You'll see for yourself when closures become useful.
Shawn Smith
Ranch Hand

Joined: Feb 22, 2011
Posts: 41
I find clojure methods useful when I don't have to retain a named object. In otherwords I'm passing on a behaviour, but don't need to retain a copy of the object that has the behaviour.

For example, with a button action:

J

The advantage in this case is that I don't have to write something else that implements ActionListener, all of the code is very visible, so for maintenance you're not traversing compilation modules to determine behavior, and I don't have a reference to something that I really don't need. If the code is complex and/or large it may be beneficial to break it out into a seperate class for readability.

Hope this make sense.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Sorry for being a nitpicker, but I want to remind everyone that the mechanism is known as a closure, and that Clojure is the name of a functional language.
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Java the language doesn't has Closures (blocks, lambdas or what they called in other languages), the closest thing to Closuers in Java is anonymous classes.
Rally Futureman
Greenhorn

Joined: Sep 28, 2011
Posts: 1
Would this go for closure?



The nameless class instance "r" is initialized, see line 24, and then the local variable lives on, without being accessible from outside.
This would work for listeners too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: clojure functors vs normal methods