Help us understand the problem. What is going on with this article?

Groovyでカード分配?アルゴリズム

More than 5 years have passed since last update.

以下のページを見て、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) == []
saba1024
ドイツでWeb系エンジニアとして働いている日本人です。 基本プログラマですがシスアドっぽいこともしています。 大体Groovy、Grails関連かサーバ関連の内容を書いています。 _(:3」∠)_ =3 ブーッ
https://doitu.info/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away