Tuesday, August 8, 2017

Lazy Permutations in Swift

Joshua Emmons:

Why is our generic type X an iterator but we force Y to conform to Collection? If you look at the nested loop in our naive example above, you’ll see that while we iterate over xs only once, we actually loop over ys a number of times (an xs.count number of times, to be precise).

IteratorProtocol allows us to iterate over a set exactly once, so it’s perfect for our X type. But only Collection guarantees us the ability to non-destructively traverse a sequence over and over. So Y 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.

Leave a Comment