aspose file tools*
The moose likes Groovy and the fly likes Example of using Groovy Closure for a different programming style Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Example of using Groovy Closure for a different programming style" Watch "Example of using Groovy Closure for a different programming style" New topic
Author

Example of using Groovy Closure for a different programming style

pino astarita
Greenhorn

Joined: Nov 14, 2006
Posts: 15
see these code (Groovy):

def execute = {
Object[] closures ->
def currentClosure = null;
closures.each() {
currentClosure = it.call(currentClosure)
}
return currentClosure
}



def cache = new ArrayList()
def elementsList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def sum = 0;

// ### ACTIVITY: NUMERO ELEMENTI LISTA > 5 ###
def ifElementNumberPassFive = { Object[] closures -> if (elementsList.size() > 5) execute(closures) }

def sumListElements = {
println "sum of element of array"
elementsList.each { sum += it }
println "sum $sum"
sum
}

def storeSumInCache = {
println 'if not present store sum in cache!';
if (!cache.contains(it)) cache.add(it);
it
}

def printSum = { println "stampo risultato somma $it" }

// ### CODE FLOW ###
ifElementNumberPassFive(sumListElements, storeSumInCache, printSum)
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

I don't understand - did you have a question about this code?

Also, please use the 'code' UBBCode tag to format source code. Reading source code without proper indentation is difficult.


JBoss In Action
pino astarita
Greenhorn

Joined: Nov 14, 2006
Posts: 15
excuse me Peter,

i don'have a real question about these code, i would know what think about the same but i think this forum isn't the correct place!

-> IF I MUST I DELETE THIS TOPIC!

Anywhere the code formatted is located after and show the use of Groovy Closure to apply a new language model pattern, the example declare a closure that call recursivly n closure passed them and i use this to execute n Closure properly:

see these code (Groovy):

Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Hi pino,

You've prompted me to publish a blog post that I hope doesn't become a Pandora's box. I think you might enjoy it, as it does something similar but slightly more complex than what you're proposing.

Chain those Crazy Closures with Currying

I think calling this chaining of closures a "pattern" is rather dangerous, particularly because a pattern is supposed to solve a specific problem in a given context. In your example, there was really no problem with writing your code normally without the chaining of closures. Switching to using the closures, one after the other only complicated the code with no perceivable advantage.

However, I do think this is a fun concept to play with and I wouldn't doubt if there are plenty of places in the world where something like this potential "pattern" would really solve something, such as in a workflow engine.


A good workman is known by his tools.
pino astarita
Greenhorn

Joined: Nov 14, 2006
Posts: 15
Hi Marc,

the propose to use the word 'pattern' correctly can be ambiguos but i would refer to the question about to tempt to solve the problem of readiblity of a software for this i've used the word 'pattern'.
Chaining different work together in the way i've described help to sintetize a worflow ,better, more unit of work related, in a better way.
Aniway would be an experimental code only this but i thing that i've proposed it incorrect way.

Aniway, Thanks for you attention! ;-) by!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Example of using Groovy Closure for a different programming style