やってみました。
IntRangeとかInteger#timesとか使ったほうがいいんだけど、無駄な計算しているよなということで、
結局Integer.metaClassをいじってしまいました。
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
def fizzbuzz = {final int arg -> | |
def res = (arg % 3 == 0? 'fizz' : '' ) + (arg % 5 == 0? 'buzz' : '' ) | |
res += res.size() == 0? arg : '' | |
res | |
} | |
Integer.metaClass.define { | |
items = {Closure c -> | |
final int num = delegate | |
def results = [] | |
def i = 0 | |
while(num > results.size()) { | |
if (c(i)) { | |
results << i | |
} | |
i += 1 | |
} | |
results | |
} | |
} | |
println 5.items { | |
fizzbuzz(it) == 'fizz' | |
} |
結果
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
[3, 6, 9, 12, 18] |
多分、もっといい書き方はきょんくんがやってくれるでしょう。
0 件のコメント:
コメントを投稿