Posted By

narkisr on 03/23/09


Tagged

style groovy compose functional


Versions (?)

Closure composition in Groovy


 / Published in: Groovy
 

Clojsure composition in Groovy (functional style programing). @http://www.ibm.com/developerworks/java/library/j-pg08235/index.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  

You need to login to post a comment.