Created
May 22, 2012 15:46
-
-
Save bufferings/2769883 to your computer and use it in GitHub Desktop.
Groovyの勉強:"Buzzになる数字をn個挙げる"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://groovy.codehaus.org/Japanese+Functional+Programming+with+Groovy
の「無限の構造」のところをコピペしてきて、こうしたら動くのかなとか思いながらグニグニしてみて、遊び倒したコードの残骸