以下のページを見て、Groovyでなるだけ関数型っぽく書いてみた。
http://qiita.com/hisui@github/items/b47c411437d60440a605
そもそも関数型じゃないのかこれじゃ?
cardDistributor.groovy
def spliter(Integer users, List cards) {
cards?.size() < users ? [] : (0 ..< users).collect {index ->
def distributionNumber = (cards?.size() / users) as Integer;
(index ..< distributionNumber*users).step(users).collect{cards.get(it)}
}
}
assert spliter(2, (1..10)) == [[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]
assert spliter(2, (1..9)) == [[1, 3, 5, 7], [2, 4, 6, 8]]
assert spliter(3, (1..5)) == [[1], [2], [3]]
assert spliter(5, (1..3)) == []
assert spliter(5, null) == []
// これを何とかスマートに解決できないだろうか・・・
// 現状のままだとNullPointerException
//assert spliter(null, (1..3)) == []
//assert spliter(null,null) == []