Skip to content

Commit a441a54

Browse files
committed
Add LazySequenceProtocol overloads that maintain laziness
1 parent 73c4a21 commit a441a54

File tree

5 files changed

+82
-8
lines changed

5 files changed

+82
-8
lines changed

Sources/Algorithms/Chain.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,19 @@ extension Sequence {
289289
Chain(base1: self, base2: other)
290290
}
291291
}
292+
293+
//===----------------------------------------------------------------------===//
294+
// lazy.chained(with:)
295+
//===----------------------------------------------------------------------===//
296+
297+
extension LazySequenceProtocol {
298+
/// Returns a new lazy sequence that iterates over this sequence, followed by
299+
/// the given sequence.
300+
public func chained<S: Sequence>(
301+
with other: S
302+
) -> LazySequence<Chain<Elements, S>>
303+
where Element == S.Element
304+
{
305+
elements.chained(with: other).lazy
306+
}
307+
}

Sources/Algorithms/Combinations.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,17 @@ extension Collection {
151151
return Combinations(self, k: k)
152152
}
153153
}
154+
155+
//===----------------------------------------------------------------------===//
156+
// lazy.combinations(count:)
157+
//===----------------------------------------------------------------------===//
158+
159+
extension LazyCollectionProtocol {
160+
/// Returns a lazy collection of combinations of this collection's elements,
161+
/// with each combination having the specificed number of elements.
162+
public func combinations(
163+
ofCount k: Int
164+
) -> LazySequence<Combinations<Elements>> {
165+
elements.combinations(ofCount: k).lazy
166+
}
167+
}

Sources/Algorithms/Cycle.swift

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,7 @@ extension Collection {
7171
public func cycled() -> Cycle<Self> {
7272
Cycle(base: self)
7373
}
74-
}
75-
76-
//===----------------------------------------------------------------------===//
77-
// repeated(count:)
78-
//===----------------------------------------------------------------------===//
79-
80-
extension Collection {
74+
8175
/// Returns a sequence that repeats the elements of this collection the
8276
/// specified number of times.
8377
///
@@ -99,3 +93,23 @@ extension Collection {
9993
repeatElement(self, count: times).joined()
10094
}
10195
}
96+
97+
//===----------------------------------------------------------------------===//
98+
// lazy.cycled()
99+
//===----------------------------------------------------------------------===//
100+
101+
extension LazyCollection {
102+
/// Returns a lazy sequence that repeats the elements of this collection
103+
/// forever.
104+
public func cycled() -> LazySequence<Cycle<Elements>> {
105+
elements.cycled().lazy
106+
}
107+
108+
/// Returns a lazy sequence that repeats the elements of this collection the
109+
/// specified number of times.
110+
public func cycled(
111+
times: Int
112+
) -> LazySequence<FlattenSequence<Repeated<Elements>>> {
113+
elements.cycled(times: times).lazy
114+
}
115+
}

Sources/Algorithms/Indexed.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ extension Indexed: RandomAccessCollection where Base: RandomAccessCollection {}
5959
extension Indexed: Equatable where Base: Equatable {}
6060
extension Indexed: Hashable where Base: Hashable {}
6161

62+
//===----------------------------------------------------------------------===//
63+
// indexed()
64+
//===----------------------------------------------------------------------===//
65+
6266
extension Collection {
6367
/// Returns a collection of pairs *(i, x)*, where *i* represents an index of
6468
/// the collection, and *x* represents an element.
@@ -79,3 +83,15 @@ extension Collection {
7983
Indexed(base: self)
8084
}
8185
}
86+
87+
//===----------------------------------------------------------------------===//
88+
// lazy.indexed()
89+
//===----------------------------------------------------------------------===//
90+
91+
extension LazyCollectionProtocol {
92+
/// Returns a lazy collection of pairs *(i, x)*, where *i* represents an index
93+
/// of the collection, and *x* represents an element.
94+
public func indexed() -> LazySequence<Indexed<Elements>> {
95+
elements.indexed().lazy
96+
}
97+
}

Sources/Algorithms/Permutations.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ extension MutableCollection
177177
}
178178

179179
//===----------------------------------------------------------------------===//
180-
// permutations(count:)
180+
// permutations(ofCount:)
181181
//===----------------------------------------------------------------------===//
182182

183183
extension Collection {
@@ -234,3 +234,17 @@ extension Collection {
234234
return Permutations(self, k: k)
235235
}
236236
}
237+
238+
//===----------------------------------------------------------------------===//
239+
// lazy.permutations(ofCount:)
240+
//===----------------------------------------------------------------------===//
241+
242+
extension LazyCollectionProtocol {
243+
/// Returns a lazy collection of the permutations of this collection of the
244+
/// specified length.
245+
public func permutations(
246+
ofCount k: Int? = nil
247+
) -> LazySequence<Permutations<Elements>> {
248+
elements.permutations(ofCount: k).lazy
249+
}
250+
}

0 commit comments

Comments
 (0)