This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Example of using Groovy Closure for a different programming style

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):

 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic