This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Groovy and the fly likes Clousers in Groovy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Languages » Groovy
Bookmark "Clousers in Groovy" Watch "Clousers in Groovy" New topic
Author

Clousers in Groovy

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Hi.
I have reading about Groovy and I didn't understand these :
1- def toTriple = {n -> n * 3}
what is -> ?
2- def f = { list, value -> list << value }
What is << value ?
3- c = { value1 -> def it = 789; [value1, it] }
Why there is semicolon after it delaration ?
Thanks for help.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

1- def toTriple = {n -> n * 3}
what is -> ?


From Groovy in Action:


Informally, a closure can be recognized as a list of statements within curly braces, like any other code block. It optionally has a list of identifiers in order to name the parameters passed to it, with an -> arrow marking the end of the list.


I've not really seen any examples like the one you have posted. This example makes the definition clearer:



And the result of this statement would be:
1
2
3

Each index of the list [1,2,3] becomes the value for entry over the iteration.

2- def f = { list, value -> list << value }
What is << value ?


In this situation the << appends the value to the list. So if you did this in the groovy console:



Your list would then become [1,2,3,4]

3- c = { value1 -> def it = 789; [value1, it] }
Why there is semicolon after it delaration ?


Because it's a declaration. If you left the semicolon off then groovy would think that [value1, it] was also part of it. I wouldn't expect to see this syntax much though.

You should pick up Groovy in Action. It's a great book.


GenRocket - Experts at Building Test Data
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

When to use the def keyword ?
What is the difference between method/variable declared with def and non-def method/variable ?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by John Todd:
When to use the def keyword ?
What is the difference between method/variable declared with def and non-def method/variable ?


John, this information is available on the Groovy website (http://groovy.codehaus.org/Blocks%2C+Closures%2C+and+Functions) and in Groovy in Action. Please don't mistake our helpfulness as compensation for you being lazy.
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Sorry Gregg.
But I swear I read that page (before you post the link) and I didn't understand when to use def (I don't digest scripting languages well).
Sorry again for disturbing.
 
GeeCON Prague 2014
 
subject: Clousers in Groovy