Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clojure functors vs normal methods

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5535
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5535
53
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic