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 thecurry()method.Let's see some
curry()action in the following sample:00.// Simple sample.01.defaddNumbers = { x, y -> x + y }02.defaddOne = addNumbers.curry(1)03.assert5== addOne(4)04.05.06.// General closure to use a filter on a list.07.deffilterList = { filter, list -> list.findAll(filter) }08.// Closure to find even numbers.09.defeven = { it %2==0}10.// Closure to find odd numbers.11.defodd = { !even(it) }12.// Other closures can be curry parameters.13.defevenFilterList = filterList.curry(even)14.defoddFilterList = 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.deffindText = { filter, handler, text ->21.text.eachLine{22.filter(it) ? handler(it) :null23.}24.}25.// Recipe for a regular expression filter.26.defregexFilter = { pattern, line -> line =~ pattern }27.28.// Create filter for searching lines with "Groovy".29.defgroovyFilter = regexFilter.curry(/Groovy/)30.// Create handler to print out line.31.defprintHandler = {println"Found in line: $it"}32.33.// Create specific closure as clone of processText to34.// search with groovyFilter and print out found lines.35.deffindGroovy = 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.
浙公网安备 33010602011771号