Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Example of using Groovy Closure for a different programming style

 
pino astarita
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pino astarita
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pino astarita
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic