阅读 53

Groovy Goodness: Add Some Curry for Taste Messages from mrhaki

Groovy Goodness: Add Some Curry for Taste - Messages from mrhaki

Groovy Goodness: Add Some Curry for Taste

Currying is a technique to create a clone of a closure and fixing values for some of the parameters. We can fix one or more parameters, depending on the number of arguments we use for the curry() method. The parameters are bound from left to right. The good thing is we can even use other closures as parameters for the curry() method.

Let's see some curry() action in the following sample:

00.// Simple sample.

01.def addNumbers = { x, y -> x + y }

02.def addOne = addNumbers.curry(1)

03.assert 5 == addOne(4)

04. 

05. 

06.// General closure to use a filter on a list.

07.def filterList = { filter, list -> list.findAll(filter) }

08.// Closure to find even numbers.

09.def even = { it % 2 == 0 }

10.// Closure to find odd numbers. 

11.def odd = { !even(it) } 

12.// Other closures can be curry parameters.

13.def evenFilterList = filterList.curry(even)

14.def oddFilterList = filterList.curry(odd)

15.assert [0,2,4,6,8] == evenFilterList(0..8)

16.assert [1,3,5,7] == oddFilterList(0..8)

17. 

18. 

19.// Recipe to find text in lines.

20.def findText = { filter, handler, text ->

21.    text.eachLine {

22.        filter(it) ? handler(it) : null

23.    }

24.}

25.// Recipe for a regular expression filter.

26.def regexFilter = { pattern, line -> line =~ pattern }

27. 

28.// Create filter for searching lines with "Groovy".

29.def groovyFilter = regexFilter.curry(/Groovy/)

30.// Create handler to print out line.

31.def printHandler = { println "Found in line: $it" }

32. 

33.// Create specific closure as clone of processText to

34.// search with groovyFilter and print out found lines.

35.def findGroovy = findText.curry(groovyFilter, printHandler)

36. 

37.// Invoke the closure.

38.findGroovy('''Groovy rules!

39.And Java?

40.Well... Groovy needs the JVM...

41.''')

42. 

43.// This will output:

44.// Found in line: Groovy rules!

45.// Foudn in line: Well... Groovy needs the JVM...

Run this script on GroovyConsole.


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐