for x in [1, 2, 3].pairwiseComparison() {
print(x) // -> .Initial(1) -> .Pair(1, 2) -> .Pair(2, 3) -> .Final(3)
}
Partitioning
for x in"hello world how are you?".characters.partition({ ($0=="") != ($1=="") }).map(String.init) {
print(x) // -> "hello" -> " " -> "world" -> " " -> "how" -> " " -> "are" -> " " -> "you?"
}
Unwrapping
for x in ["1.2", "4.6", "blah", "2.9"].map({ Float($0) }).unwrap() {
print(x) // -> 1.2 -> 4.6 -> 2.9
}
Peeking
var generator = PeekableSequence(["Dog", "Cat", "Cow", "Fish"]).generate()
whilelet this = generator.next() {
guardlet next = generator.peek() else { continue }
if this.characters.first== next.characters.first {
print("The word after \(this) starts with the same letter.")
// -> The word after Cat starts with the same letter.
}
}
Lookahead
var generator = LookaheadSequence([0, 1, 0, 7, 1, 1, 0, 1]).generate()
whilelet value = generator.next() {
if value ==0 {
let ahead = generator.lookahead(0..<3)
print(ahead) // -> [1, 0, 7] -> [7, 1, 1] -> [1]
}
}
The Tidelift Subscription provides access to a continuously curated stream of human-researched and maintainer-verified data on open source packages and their licenses, releases, vulnerabilities, and development practices.