From 2c16fcaa999443dcbb47ade8ce1aa93ee9c14698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 28 Jan 2018 15:50:43 +0100 Subject: [PATCH] Cursor cleanup --- GRDB/Core/Cursor.swift | 71 ++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 41 deletions(-) diff --git a/GRDB/Core/Cursor.swift b/GRDB/Core/Cursor.swift index b271c8b43..9c96e40dc 100644 --- a/GRDB/Core/Cursor.swift +++ b/GRDB/Core/Cursor.swift @@ -17,7 +17,7 @@ extension Sequence { /// Returns a cursor over the concatenated results of mapping transform /// over self. - public func flatMap(_ transform: @escaping (Iterator.Element) throws -> SegmentOfResult) -> FlattenCursor, SegmentOfResult>> { + public func flatMap(_ transform: @escaping (Iterator.Element) throws -> SegmentOfResult) -> FlattenCursor, SegmentOfResult>> { return IteratorCursor(self).flatMap(transform) } } @@ -59,19 +59,11 @@ extension Set { /// - A cursor can not be repeated. /// /// The protocol comes with default implementations for many operations similar -/// to those defined by Swift's LazySequenceProtocol: -/// -/// - `func contains(Self.Element)` -/// - `func contains(where: (Self.Element) throws -> Bool)` -/// - `func enumerated()` -/// - `func filter((Self.Element) throws -> Bool)` -/// - `func first(where: (Self.Element) throws -> Bool)` -/// - `func flatMap((Self.Element) throws -> ElementOfResult?)` -/// - `func flatMap((Self.Element) throws -> SegmentOfResult)` -/// - `func forEach((Self.Element) throws -> Void)` -/// - `func joined()` -/// - `func map((Self.Element) throws -> T)` -/// - `func reduce(Result, (Result, Self.Element) throws -> Result)` +/// to those defined by Swift's Sequence protocol: `contains`, `dropFirst`, +/// `dropLast`, `drop(while:)`, `enumerated`, `filter`, `first`, `flatMap`, +/// `forEach`, `joined`, `joined(separator:)`, `max`, `max(by:)`, `min`, +/// `min(by:)`, `map`, `prefix`, `prefix(while:)`, `reduce`, `reduce(into:)`, +/// `suffix`. public protocol Cursor : class { /// The type of element traversed by the cursor. associatedtype Element @@ -313,8 +305,6 @@ extension Cursor { /// - Returns: A cursor starting at the beginning of this cursor /// with at most `maxLength` elements. public func prefix(_ maxLength: Int) -> PrefixCursor { - // TODO: test that [1,2,3,4].prefix(2).prefix(1) is equivalent to - // [1,2,3,4].prefix(1). return PrefixCursor(self, maxLength: maxLength) } @@ -458,7 +448,7 @@ extension Cursor where Element: Cursor { extension Cursor where Element: Sequence { /// Returns the elements of this cursor of sequences, concatenated. - public func joined() -> FlattenCursor>> { + public func joined() -> FlattenCursor>> { return flatMap { $0 } } } @@ -496,6 +486,8 @@ extension Cursor where Element: StringProtocol { /// having the same Element type, hiding the specifics of the underlying /// cursor. public class AnyCursor : Cursor { + private let element: () throws -> Element? + /// Creates a cursor that wraps a base cursor but whose type depends only on /// the base cursor’s element type public init(_ base: C) where C.Element == Element { @@ -513,12 +505,10 @@ public class AnyCursor : Cursor { public func next() throws -> Element? { return try element() } - - private let element: () throws -> Element? } public final class DropFirstCursor : Cursor { - private var base: Base + private let base: Base private let limit: Int private var dropped: Int = 0 @@ -543,8 +533,8 @@ public final class DropFirstCursor : Cursor { /// A cursor whose elements consist of the elements that follow the initial /// consecutive elements of some base cursor that satisfy a given predicate. public final class DropWhileCursor : Cursor { - private var base: Base - private var predicate: (Base.Element) throws -> Bool + private let base: Base + private let predicate: (Base.Element) throws -> Bool private var predicateHasFailed = false init(_ base: Base, predicate: @escaping (Base.Element) throws -> Bool) { @@ -580,9 +570,12 @@ public final class DropWhileCursor : Cursor { /// // Prints: "0: foo" /// // Prints: "1: bar" public final class EnumeratedCursor : Cursor { + private let base: Base + private var index: Int + init(_ base: Base) { - self.index = 0 self.base = base + self.index = 0 } /// Advances to the next element and returns it, or nil if no next @@ -593,14 +586,14 @@ public final class EnumeratedCursor : Cursor { defer { index += 1 } return (index, element) } - - private var index: Int - private var base: Base } /// A cursor whose elements consist of the elements of some base cursor that /// also satisfy a given predicate. public final class FilterCursor : Cursor { + private let base: Base + private let isIncluded: (Base.Element) throws -> Bool + init(_ base: Base, _ isIncluded: @escaping (Base.Element) throws -> Bool) { self.base = base self.isIncluded = isIncluded @@ -617,9 +610,6 @@ public final class FilterCursor : Cursor { } return nil } - - private let base: Base - private let isIncluded: (Base.Element) throws -> Bool } /// A cursor consisting of all the elements contained in each segment contained @@ -627,6 +617,9 @@ public final class FilterCursor : Cursor { /// /// See Cursor.joined(), Cursor.flatMap(_:), Sequence.flatMap(_:) public final class FlattenCursor : Cursor where Base.Element: Cursor { + private let base: Base + private var inner: Base.Element? + init(_ base: Base) { self.base = base } @@ -645,9 +638,6 @@ public final class FlattenCursor : Cursor where Base.Element: Curs self.inner = inner } } - - private var inner: Base.Element? - private let base: Base } /// A Cursor whose elements consist of those in a Base Cursor passed through a @@ -655,6 +645,9 @@ public final class FlattenCursor : Cursor where Base.Element: Curs /// /// See Cursor.map(_:) public final class MapCursor : Cursor { + private let base: Base + private let transform: (Base.Element) throws -> Element + init(_ base: Base, _ transform: @escaping (Base.Element) throws -> Element) { self.base = base self.transform = transform @@ -667,15 +660,12 @@ public final class MapCursor : Cursor { guard let element = try base.next() else { return nil } return try transform(element) } - - private let base: Base - private let transform: (Base.Element) throws -> Element } /// A cursor that only consumes up to `n` elements from an underlying /// `Base` cursor. public final class PrefixCursor : Cursor { - private var base: Base + private let base: Base private let maxLength: Int private var taken = 0 @@ -700,8 +690,8 @@ public final class PrefixCursor : Cursor { /// A cursor whose elements consist of the initial consecutive elements of /// some base cursor that satisfy a given predicate. public final class PrefixWhileCursor : Cursor { - private var base: Base - private var predicate: (Base.Element) throws -> Bool + private let base: Base + private let predicate: (Base.Element) throws -> Bool private var predicateHasFailed = false init(_ base: Base, predicate: @escaping (Base.Element) throws -> Bool) { @@ -723,7 +713,8 @@ public final class PrefixWhileCursor : Cursor { /// A Cursor whose elements are those of a sequence iterator. public final class IteratorCursor : Cursor { - + private var base: Base + /// Creates a cursor from a sequence iterator. public init(_ base: Base) { self.base = base @@ -740,7 +731,5 @@ public final class IteratorCursor : Cursor { public func next() -> Base.Element? { return base.next() } - - private var base: Base }