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?

kotlinのchunkedをswiftでも使えるようにしたい

Posted at

はじめに

KotlinにはCollectionに対して指定数に切り分ける関数が存在しますが、Swiftには存在しないようなので自前で実装していきます

本文

以下コードになります
拡張関数を書くだけでも結構記述方法が違います

extension Array {
    func chunked(into size: Int) -> [[Element]] {
        stride(from: 0, to: count, by: size).map { index in
            Array(self[index..<Swift.min(index + size, count)])
        }
    }
}

最後に

どちらも触ってるとどっちもある意味で理解というか納得できる記述方法があって面白いなと思う日々です
どなたかのお役に立てたら幸いです

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?