Lazy Permutations in Swift
Why is our generic type
X
an iterator but we forceY
to conform toCollection
? If you look at the nested loop in our naive example above, you’ll see that while we iterate overxs
only once, we actually loop overys
a number of times (anxs.count
number of times, to be precise).
IteratorProtocol
allows us to iterate over a set exactly once, so it’s perfect for ourX
type. But onlyCollection
guarantees us the ability to non-destructively traverse a sequence over and over. SoY
must be a little more constrained.[…]
And so, finally, we can lazily iterate over every ordered pair of our collections — all while only storing the collections themselves, not their product.
1 Comment RSS · Twitter
Wow, his articles are very good to learn new things about both Swift and Mac programming. Nearly all of the "Tips & Tricks" posts are very helpful to me. Only drawback is that his blogging site doesn't seem to provide an overview of all articles - instead one has to scroll down on a huge page to find each one's headline.