You can get your collection in subsequences of two and reduce them into a dictionary:
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 kvs = ["key1", "value1", "key2", "value2"]
let dictionary = kvs
.unfoldSubSequences(limitedTo: 2)
.reduce(into: [:]) {
guard $1.count == 2, let key = $1.first, let value = $1.last else { return }
$0[key] = value
}
dictionary // ["key1": "value1", "key2": "value2"]
Another approach using stride. This assumes your collection has an even number of elements:
let kvs = ["key1", "value1", "key2", "value2"]
let dictionary = stride(from: 0, to: kvs.count, by: 2)
.reduce(into: [:]) { $0[kvs[$1]] = kvs[$1+1] }
dictionary // ["key1": "value1", "key2": "value2"]
stride,map,filteretc, but you will still be doing the same "go through the array by a step of two, and get n and n+1 for key and value". I don't think you can avoid writing the number "2" and getting n and n + 1, without sacrificing a lot of readability.