Skip to content

Commit 806341a

Browse files
committed
Remove the index property for Intersperse.Index because the separator case doesn’t use it
This moves the property to be an associated value on the element case so that we can provide different names when we switch over the values.
1 parent 0b7a16d commit 806341a

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

Sources/Algorithms/Intersperse.swift

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,41 +56,40 @@ extension Intersperse: Sequence {
5656

5757
extension Intersperse: Collection where Base: Collection {
5858
public struct Index: Comparable {
59-
enum Kind: Comparable {
60-
case element
59+
enum Representation: Comparable {
60+
case element(Base.Index)
6161
case separator(next: Base.Index)
6262
}
63-
let index: Base.Index
64-
let kind: Kind
63+
let representation: Representation
6564

6665
public static func < (lhs: Index, rhs: Index) -> Bool {
67-
(lhs.index, lhs.kind) < (rhs.index, rhs.kind)
66+
lhs.representation < rhs.representation
6867
}
6968
}
7069

7170
public var startIndex: Index {
72-
Index(index: base.startIndex, kind: .element)
71+
Index(representation: .element(base.startIndex))
7372
}
7473

7574
public var endIndex: Index {
76-
Index(index: base.endIndex, kind: .element)
75+
Index(representation: .element(base.endIndex))
7776
}
7877

7978
public func index(after i: Index) -> Index {
80-
switch i.kind {
81-
case .element:
82-
let next = base.index(after: i.index)
79+
switch i.representation {
80+
case let .element(index):
81+
let next = base.index(after: index)
8382
return next == base.endIndex
8483
? endIndex
85-
: Index(index: i.index, kind: .separator(next: next))
86-
case .separator(let next):
87-
return Index(index: next, kind: .element)
84+
: Index(representation: .separator(next: next))
85+
case let .separator(next):
86+
return Index(representation: .element(next))
8887
}
8988
}
9089

9190
public subscript(position: Index) -> Element {
92-
switch position.kind {
93-
case .element: return base[position.index]
91+
switch position.representation {
92+
case .element(let index): return base[index]
9493
case .separator: return separator
9594
}
9695
}
@@ -100,13 +99,13 @@ extension Intersperse: BidirectionalCollection
10099
where Base: BidirectionalCollection
101100
{
102101
public func index(before i: Index) -> Index {
103-
switch i.kind {
104-
case .element where i.index == base.endIndex:
105-
return Index(index: base.index(before: i.index), kind: .element)
106-
case .element:
107-
return Index(index: base.index(before: i.index), kind: .separator(next: i.index))
108-
case .separator:
109-
return Index(index: i.index, kind: .element)
102+
switch i.representation {
103+
case let .element(index) where index == base.endIndex:
104+
return Index(representation: .element(base.index(before: index)))
105+
case let .element(index):
106+
return Index(representation: .separator(next: index))
107+
case let .separator(next):
108+
return Index(representation: .element(base.index(before: next)))
110109
}
111110
}
112111
}

0 commit comments

Comments
 (0)