Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

Iterate over collection two at a time in Swift

Say I have an array [1, 2, 3, 4, 5]. How can I iterate two at a time?

Iteration 1: (1, 2)
Iteration 2: (3, 4)
Iteration 3: (5, nil)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use a progression loop called stride(to:, by:) to iterate over your elements every n elements:

let array = Array(1...5)

let pairs = stride(from: 0, to: array.endIndex, by: 2).map {
    (array[$0], $0 < array.index(before: array.endIndex) ? array[$0.advanced(by: 1)] : nil)
}   // [(.0 1, {some 2}), (.0 3, {some 4}), (.0 5, nil)]

print(pairs)  // "[(1, Optional(2)), (3, Optional(4)), (5, nil)]
"

To iterate your collection subsequences instead of tuples:

extension Collection {
    func unfoldSubSequences(limitedTo maxLength: Int) -> UnfoldSequence<SubSequence,Index> {
        sequence(state: startIndex) { start in
            guard start < self.endIndex else { return nil }
            let end = self.index(start, offsetBy: maxLength, limitedBy: self.endIndex) ?? self.endIndex
            defer { start = end }
            return self[start..<end]
        }
    }
}

let array = Array(1...5)
for subsequence in array.unfoldSubSequences(limitedTo: 2) {
    print(subsequence)  // [1, 2] [3, 4] [5]
}

This would work on any kind of collection:

let string = "12345"
for substring in string.unfoldSubSequences(limitedTo: 2) {
    print(substring)  // "12" "34" "5"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...