Extending
Chain two collections end-to-end, or repeat a collection forever or a specific number of times.
Overview
Chaining two collections
let letters = chain("abcd", "EFGH")
// String(letters) == "abcdEFGH"
for (num, letter) in zip((1...3).cycled(), letters) {
print(num, letter)
}
// 1 a
// 2 b
// 3 c
// 1 d
// 2 E
// 3 F
// 1 G
// 2 H
Chaining Two Collections
func chain<S1, S2>(S1, S2
) -> Chain2Sequence<S1, S2> Returns a new sequence that iterates over the two given sequences, one followed by the other.
Cycling a Collection
func cycled(
) -> CycledSequence<Self> Returns a sequence that repeats the elements of this collection forever.
func cycled(times: Int
) -> CycledTimesCollection<Self> Returns a sequence that repeats the elements of this collection the specified number of times.
Supporting Types
struct Chain2Sequence<Base1, Base2>
A concatenation of two sequences with the same element type.
struct CycledSequence<Base>
A collection wrapper that repeats the elements of a base collection.
struct CycledTimesCollection<Base>
A collection wrapper that repeats the elements of a base collection for a finite number of times.