From 28bbed877656651272e5cc019c2019bcfa21a92c Mon Sep 17 00:00:00 2001 From: Oliver Atkinson Date: Fri, 6 Nov 2020 10:13:29 +0000 Subject: [PATCH] Add implementation for Sequence iterator --- Guides/Stride.md | 6 ++--- Sources/Algorithms/Stride.swift | 28 +++++++++++++++++--- Tests/SwiftAlgorithmsTests/StrideTests.swift | 4 +-- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/Guides/Stride.md b/Guides/Stride.md index 874e742..0c46ecc 100644 --- a/Guides/Stride.md +++ b/Guides/Stride.md @@ -5,7 +5,7 @@ A type that steps over a collection’s elements by the specified amount. -This is available through the `striding(by:)` method on any `Collection`. +This is available through the `striding(by:)` method on any `Sequence`. ```swift (0...10).striding(by: 2) // == [0, 2, 4, 6, 8, 10] @@ -18,11 +18,11 @@ The stride amount must be a positive value. ## Detailed Design -The `striding(by:)` method is declared as a `Collection` extension, and returns a +The `striding(by:)` method is declared as a `Sequence` extension, and returns a `Stride` type: ```swift -extension Collection { +extension Sequence { public func striding(by step: Int) -> Stride } ``` diff --git a/Sources/Algorithms/Stride.swift b/Sources/Algorithms/Stride.swift index 1dbd2ae..ee8a5ab 100644 --- a/Sources/Algorithms/Stride.swift +++ b/Sources/Algorithms/Stride.swift @@ -13,7 +13,7 @@ // striding(by:) //===----------------------------------------------------------------------===// -extension Collection { +extension Sequence { /// Returns a collection stepping through the elements every `step` starting /// at the first value. Any remainders of the stride will be trimmed. /// @@ -32,7 +32,7 @@ extension Collection { } } -public struct Stride { +public struct Stride { let base: Base let stride: Int @@ -50,7 +50,29 @@ extension Stride { } } -extension Stride: Collection { +extension Stride: Sequence { + + public struct Iterator: IteratorProtocol { + + var iterator: Base.Iterator + let stride: Int + + public mutating func next() -> Base.Element? { + defer { + for _ in 0..<(stride - 1) { + guard iterator.next() != nil else { break } + } + } + return iterator.next() + } + } + + public func makeIterator() -> Stride.Iterator { + Iterator(iterator: base.makeIterator(), stride: stride) + } +} + +extension Stride: Collection where Base: Collection { public struct Index: Comparable { diff --git a/Tests/SwiftAlgorithmsTests/StrideTests.swift b/Tests/SwiftAlgorithmsTests/StrideTests.swift index 3a54233..6d53c1d 100644 --- a/Tests/SwiftAlgorithmsTests/StrideTests.swift +++ b/Tests/SwiftAlgorithmsTests/StrideTests.swift @@ -15,7 +15,7 @@ import Algorithms final class StridingTests: XCTestCase { func testStride() { - let a = (0...10) + let a = 0...10 XCTAssertEqualSequences(a.striding(by: 1), (0...10)) XCTAssertEqualSequences(a.striding(by: 2), [0, 2, 4, 6, 8, 10]) XCTAssertEqualSequences(a.striding(by: 3), [0, 3, 6, 9]) @@ -48,7 +48,7 @@ final class StridingTests: XCTestCase { XCTAssertEqual(a[i], 2) a.formIndex(before: &i) XCTAssertEqual(a[i], 0) -// a.formIndex(before: &i) // Precondition failed: Advancing past start index +// a.formIndex(before: &i) // Precondition failed: Incrementing past start index // a.index(after: a.endIndex) // Precondition failed: Advancing past end index }