0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2013-10-09

以下のページを見て、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) == []
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?