1
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.

SwiftでRubyのeach_cons, each_sliceをする

Last updated at Posted at 2019-04-18

Swift で、 Ruby の each_cons(n) みたいに、n個ずつずらしながら扱いたいときのTips。

コード

extension Array {
    
    func eachPair() -> [(Element, Element)] {
        return zip(self, self.dropFirst()).map({ ($0.0, $0.1) })
    }
    
    func eachCons(_ n: Int) -> [ArraySlice<Element>] {
        return indices.dropLast(n-1).map({ self[$0...$0+n-1] })
    }
    
    func eachSlice(_ n: Int) -> [ArraySlice<Element>] {
        return stride(from: 0, through: count - 1, by: n).map({ self[($0..<$0+n).clamped(to: indices)] })
    }
}

使用

let sample = [1, 2, 3, 4, 5, 6]

sample.eachPair()   // [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]

sample.eachCons(3)  // [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]]
sample.eachCons(6)  // [[1, 2, 3, 4, 5, 6]]
sample.eachCons(7)  // []

sample.eachSlice(2) // [[1, 2], [3, 4], [5, 6]]
sample.eachSlice(4) // [[1, 2, 3, 4], [5, 6]]
sample.eachSlice(7) // [[1, 2, 3, 4, 5, 6]]

eachPair はタプルで返るようになってます。
eachCons で大きい値を与えた場合、空の配列が返ります。

補足

  • eachCons(0) など0以下の値を与えるとエラーになるので、工夫しても良いかも。
  • eachPair() は、 Ruby で言う each_pair とは異なるので注意。
  • extension は Collection ではなく Array に対して書いています。 Dictionary などには使用しないので。

環境

  • Swift 5.0

参考

1
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
1
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?