Skip to content

Instantly share code, notes, and snippets.

@Dierk
Created April 22, 2016 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dierk/f28befa736c4c1508235e7a03a486cae to your computer and use it in GitHub Desktop.
Save Dierk/f28befa736c4c1508235e7a03a486cae to your computer and use it in GitHub Desktop.
// Haskell-style iterate function in Groovy
Closure iterate(def value, Closure nextValue) {
return { value = nextValue value }
}
// use for iteration
def iter = iterate(0) { it + 1}
5.times {
println iter()
}
// use for fixpoint calculation
def fix = iterate(1.0) { Math.cos it}
while (fix() != fix()) {}
println fix()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment