开发者

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?

开发者_StackOverflow中文版

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;

  1. The basic curry method which starts currying the parameters of a Closure from the left most parameter
  2. Then there is the rcurry method, which starts currying the parameters from the right
  3. 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 :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜