Skip to content

Instantly share code, notes, and snippets.

@nobeans
Created March 6, 2015 02:18
Show Gist options
  • Save nobeans/ffdd70d4629e5afe02a9 to your computer and use it in GitHub Desktop.
Save nobeans/ffdd70d4629e5afe02a9 to your computer and use it in GitHub Desktop.
Composition of functions by Groovy
def by8 = { it * 8 }
def plus3 = { it + 3 }
def devide2 = { it / 2 }
// two closures
assert (by8 << plus3).call(3) == 48
assert (plus3 >> by8).call(3) == 48
assert (plus3 >> by8)(3) == 48
// three closures
assert (plus3 >> by8 >> devide2)(3) == 24
assert (devide2 << by8 << plus3)(3) == 24
assert (devide2 >> by8 << plus3).call(3) == 24
assert ((devide2 >> by8) << plus3).call(3) == 24
assert (devide2 >> (by8 << plus3)).call(3) == 36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment