@Loooop: Now I don't think adjacentPairs(circular: true)
will pass the review: in the mind of many members of the forum, ad-hoc boolean arguments are not "swifty". While I wholeheartedly support the idea, it needs polish before we can convince the community.
First, what if that feature would not exist, and one wants to create his own circular collection of adjacent pairs?
Well, in the naive and straightforward way, it's painful. One can't conveniently add an element to a sequence unless that sequence is something like an array:
let values = [1, 2, 3]
var pairs = Array(values.adjacentPairs())
if let first = values.first {
pairs.append((values.last!, first))
}
print(pairs) // [(1,2), (2,3), (3,1)]
This code is uneasy to read, and loses track of the goal. On top of that:
- we lose the lazy nature of the resulting sequence.
- we lose support for single-pass sequences
- we have to check for emptiness
- there is a force unwrap in sight and some will turn pale.
This explains which it is useful for the standard library to provide the feature, and free the developers from thinking too much about it.
Now, how should it look, since a boolean parameter is not the way to go?
Well, what about another method?
var pairs = values.circularAdjacentPairs()
var pairs = values.loopingAdjacentPairs()
var pairs = values.cyclingAdjacentPairs()