Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Apache Groovy
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Functional programming=== Although Groovy is mostly an object-oriented language, it also offers [[functional programming]] features. ====Closures==== According to Groovy's documentation: "Closures in Groovy work similar to a 'method pointer', enabling code to be written and run in a later point in time".<ref>{{cite web |url=http://groovy.codehaus.org/Closures |title=Groovy - Closures |url-status=dead |archive-url=https://web.archive.org/web/20120522213410/http://groovy.codehaus.org/Closures |archive-date=2012-05-22 }}</ref> Groovy's closures support free variables, i.e. variables that have not been explicitly passed as a parameter to it, but exist in its declaration context, [[partial application]] (that it terms '[[currying]]'<ref name=":0" />), delegation, implicit, typed and untyped parameters. When working on Collections of a determined type, the closure passed to an operation on the collection can be inferred: <syntaxhighlight lang="groovy"> list = [1, 2, 3, 4, 5, 6, 7, 8, 9] /* * Non-zero numbers are coerced to true, so when it % 2 == 0 (even), it is false. * The type of the implicit "it" parameter can be inferred as an Integer by the IDE. * It could also be written as: * list.findAll { Integer i -> i % 2 } * list.findAll { i -> i % 2 } */ def odds = list.findAll { it % 2 } assert odds == [1, 3, 5, 7, 9] </syntaxhighlight> A group of expressions can be written in a closure block without reference to an implementation and the responding object can be assigned at a later point using delegation: <syntaxhighlight lang="groovy"> // This block of code contains expressions without reference to an implementation def operations = { declare 5 sum 4 divide 3 print } </syntaxhighlight> <syntaxhighlight lang="groovy"> /* * This class will handle the operations that can be used in the closure above. Another class * could be declared having the same methods, but using, for example, webservice operations * in the calculations. */ class Expression { BigDecimal value /* * Though an Integer is passed as a parameter, it is coerced into a BigDecimal, as was * defined. If the class had a 'declare(Integer value)' method, it would be used instead. */ def declare(BigDecimal value) { this.value = value } def sum(BigDecimal valueToAdd) { this.value += valueToAdd } def divide(BigDecimal divisor) { this.value /= divisor } def propertyMissing(String property) { if (property == "print") println value } } </syntaxhighlight> <syntaxhighlight lang="groovy"> // Here is defined who is going to respond the expressions in the block of code above. operations.delegate = new Expression() operations() </syntaxhighlight> ====Curry==== Usually called ''[[partial application]]'',<ref name=":0">[http://programmers.stackexchange.com/questions/152868/does-groovy-call-partial-application-currying "Does groovy call partial application 'currying']", 10 Aug 2013</ref> this Groovy feature allows closures' parameters to be set to a default parameter in any of their arguments, creating a new closure with the bound value. Supplying one argument to the <code>curry()</code> method will fix argument one. Supplying N arguments will fix arguments 1 .. N. <syntaxhighlight lang="groovy"> def joinTwoWordsWithSymbol = { symbol, first, second -> first + symbol + second } assert joinTwoWordsWithSymbol('#', 'Hello', 'World') == 'Hello#World' def concatWords = joinTwoWordsWithSymbol.curry(' ') assert concatWords('Hello', 'World') == 'Hello World' def prependHello = concatWords.curry('Hello') //def prependHello = joinTwoWordsWithSymbol.curry(' ', 'Hello') assert prependHello('World') == 'Hello World' </syntaxhighlight> Curry can also be used in the reverse direction (fixing the last N arguments) using <code>rcurry()</code>. <syntaxhighlight lang="groovy"> def power = { BigDecimal value, BigDecimal power -> value**power } def square = power.rcurry(2) def cube = power.rcurry(3) assert power(2, 2) == 4 assert square(4) == 16 assert cube(3) == 27 </syntaxhighlight> Groovy also supports [[lazy evaluation]],<ref>{{cite web |url=http://groovy.codehaus.org/Lazy+transformation |title=Groovy - Lazy Transformation |access-date=2012-10-07 |archive-url=https://web.archive.org/web/20121008091312/http://groovy.codehaus.org/Lazy+transformation |archive-date=2012-10-08 |url-status=dead }}</ref><ref>{{cite web|url=http://ndpar.blogspot.com.br/2011/02/lazy-lists-in-groovy.html|title=Side Notes: Lazy lists in Groovy|date=3 Feb 2011}}</ref> [[Fold (higher-order function)|reduce/fold]],<ref>{{cite web |url=http://bendoerr.me/posts/2011-06-20-groovy-inject.html |title=Groovy's Fold |date=20 Jun 2011 |access-date=12 February 2015 |archive-url=https://web.archive.org/web/20150213033355/http://bendoerr.me/posts/2011-06-20-groovy-inject.html |archive-date=13 February 2015 |url-status=dead }}</ref> [[Data structure|infinite structure]]s and [[immutability]],<ref>{{cite web |url=http://www.slideshare.net/arturoherrero/functional-programming-with-groovy |title=Functional Programming with Groovy |date=5 Nov 2011}}</ref> among others.<ref>{{cite web |url=http://groovy.codehaus.org/Functional+Programming+with+Groovy |title=Function programming in Groovy |access-date=2012-10-07 |archive-url=https://web.archive.org/web/20121008095622/http://groovy.codehaus.org/Functional+Programming+with+Groovy |archive-date=2012-10-08 |url-status=dead }}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)