-
-
Save derekcoder/26e5c6ec8083c23b842c52f79f10da94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct BoundedSequence<Base>: Sequence, IteratorProtocol where Base: Sequence { | |
public struct Boundary: Equatable { | |
public let isStart: Bool | |
public let isEnd: Bool | |
} | |
private var _iterator: Base.Iterator | |
private var _previous: Base.Element? | |
private var _current: Base.Element? | |
private var _next: Base.Element? | |
public init(_ base: Base) { | |
_iterator = base.makeIterator() | |
_previous = nil | |
_current = _iterator.next() | |
_next = _iterator.next() | |
} | |
public mutating func next() -> (Boundary, Base.Element)? { | |
defer { | |
(_previous, _current, _next) = (_current, _next, _iterator.next()) | |
} | |
switch (_previous, _current, _next) { | |
case let (nil, element?, nil): | |
return (Boundary(isStart: true, isEnd: true), element) | |
case let (nil, element?, _): | |
return (Boundary(isStart: true, isEnd: false), element) | |
case let (_, element?, nil): | |
return (Boundary(isStart: false, isEnd: true), element) | |
case let (_, element?, _): | |
return (Boundary(isStart: false, isEnd: false), element) | |
default: | |
return nil | |
} | |
} | |
} | |
extension Sequence { | |
func bounded() -> BoundedSequence<Self> { | |
return BoundedSequence(self) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = [1, 2, 3, 4, 5] | |
for (boundary, element) in array.bounded() { | |
if boundary.isStart { | |
print("START") | |
} | |
print(element) | |
if boundary.isEnd { | |
print("END") | |
} | |
} | |
/* | |
Prints: | |
START | |
1 | |
2 | |
3 | |
4 | |
5 | |
END | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment