Last active
December 22, 2023 15:30
-
-
Save mnvr/d67dc13f1c19c1aa36050f7b7f2cbbf8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// All the ways to zip. | |
/// | |
/// The Swift standard library doesn't seem to have a way to cycle an element so | |
/// that it can be paired with something else in a zip. These are the ways I've | |
/// found to achieve the same effect. The last one, `.map({($0, z)})` might be | |
/// the most idiomatic Swift (and is also the shortest one to express in Swift). | |
let xs = [1, 2, 3] | |
let y = 1...10 | |
for (x, y) in zip(xs, repeatElement(y, count: .max)) { | |
print(x, y) | |
} | |
for (x, y) in zip(xs, sequence(first: y) { $0 }) { | |
print(x, y) | |
} | |
for (x, y) in xs.map({ ($0, y) }) { | |
print(x, y) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment