Groovy web console

subscribe to the feed Subscribe
to this
site
Closure composition (via #groovywebconsole)
tweet this snippet Tweet
this
script

Closure composition

Published 8 years ago by mrhaki with tags groovy18 closure
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def convert = { new Expando(language: it) }
def upper = { it.toUpperCase() }

// Composition.
def upperConvert = convert << upper

def languages = ['Groovy', 'Scala', 'Clojure'].collect(upperConvert)
println languages // Output: [{language=GROOVY}, {language=SCALA}, {language=CLOJURE}]
assert languages[0].language == 'GROOVY'
assert languages[1].language == 'SCALA'
assert languages[2].language == 'CLOJURE'

// Reverse composition.
def lastLetter = { it[-1] }
def firstLetters = ['Groovy', 'Clojure', 'Scala'].collect(upper >> lastLetter)
assert firstLetters.join() == 'YEA'