Skip to content

Instantly share code, notes, and snippets.

@bufferings
Created May 22, 2012 15:46
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 bufferings/2769883 to your computer and use it in GitHub Desktop.
Save bufferings/2769883 to your computer and use it in GitHub Desktop.
Groovyの勉強:"Buzzになる数字をn個挙げる"
class LazyList {
def car
private Closure cdr
def LazyList next() { cdr() }
def List take(n) {
def r = []; def l = this
(1..n).each{ r << l.car; l++ }
return r
}
def LazyList filter(Closure pred) {
return pred(car) ? new LazyList(car:car, cdr:{ next().filter(pred) }): next().filter(pred)
}
}
def integers(n) { new LazyList(car:n, cdr:{ integers(n+1) }) }
def fizzBuzz = {it%15?it%3?it%5?it:'Buzz':'Fizz':'FizzBuzz'}
assert [5,10,20,25,35,40,50,55,65,70] == integers(1).filter{ fizzBuzz(it) == 'Buzz' }.take(10)
@bufferings
Copy link
Author

http://groovy.codehaus.org/Japanese+Functional+Programming+with+Groovy

の「無限の構造」のところをコピペしてきて、こうしたら動くのかなとか思いながらグニグニしてみて、遊び倒したコードの残骸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment