Functional programming in Groovy; Apart from curry method:?
Last night i have read about the curry()
method in Groovy and i felt the feeling of functional programming, with the use of this curry()
method.
As a novice in Groovy language, are there any methods in Groovy which provides functional programming capabilities as curry()
method does?
It will be good, if those methods are been explained with an simple example. Thanks in advance.
There's also Closure composition
def plus2 = { it + 2 }
def times3 = { it * 3 }
def composed1 = plus2 << times3
assert composed1(3) == 11
And you can use the Method Reference operator &
to get a reference to a class method, which you can then use with currying or composition.
ie:
def parseIntRef = Integer.&parseInt
def binaryParse = parseIntRef.rcurry( 2 )
def hexParse = parseIntRef.rcurry( 16 )
assert binaryParse( '110' ) == 6
assert hexParse( '0A' ) == 10
There are 3 forms of curry
for closures;
- The basic
curry
method which starts currying the parameters of a Closure from the left most parameter - Then there is the
rcurry
method, which starts currying the parameters from the right - And finally, there is
ncurry
which starts at the index specified by you.
All 3 of those curry methods are well described in the documentation if you follow the links :-)
精彩评论