Closure composition in Groovy


/ Published in: Groovy
Save to your folder(s)

Clojsure composition in Groovy (functional style programing).
@http://www.ibm.com/developerworks/java/library/j-pg08235/index.html


Copy this code and paste it in your HTML
  1. def multiply = { x, y -> return x * y }
  2. // closure
  3. def triple = multiply.curry(3)
  4. // triple = { y -> return 3 * y }
  5. def quadruple = multiply.curry(4)
  6. // quadruple = { y -> return 4 * y }
  7. def composition = { f, g, x -> return f(g(x)) }
  8. def twelveTimes = composition.curry(triple, quadruple)
  9. def threeDozen = twelveTimes(3)
  10. println "threeDozen: ${threeDozen}"
  11. // threeDozen: 36

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.