Swift sequences: The art of being lazy

⚠️ Deprecated

For an up-to-date version of this article — read it on Swift by Sundell instead, where you can also find over 100 articles about Swift. This Medium version has not been updated for the latest version of Swift, or with other improvements, such as syntax highlighting.

When creating lists and sequences of objects in Swift, most of the time we use the Array data structure. While arrays have a huge benefit in being easy to use, and something more or less every programmer on the planet knows about, they do require you to create all of your elements up front.

When dealing with smaller datasets, where each member isn’t very expensive to construct, this is not a problem. However, when this is not true, you can get some pretty big performance benefits from implementing your own lazily evaluated sequence, instead of using an array.

Let’s say we want to load a sequence of models from a local database:

Potentially our database could contain a large set of records, and for each model we need to hit the disk to actually load its data, so we don’t want to load everything at once. To make this happen, we’re going to replace the array return type with our own custom sequence.

Thanks to Swift’s protocol-oriented nature, defining your own sequences is quite easy. It also — thanks to protocol extensions — gives you access to all the APIs that you can use on the standard library-provided sequences like Array, Set or Dictionary, without having to write any code for it.

We start by creating a struct for our sequence, and make it conform to the Sequence protocol:

As you can see, all we need to do to conform to Sequence, is to be able to act as a factory for creating iterators. An iterator is what Swift actually uses to iterate over our sequence, like in a for-loop or forEach() call.

For our iterator, we’re going to keep loading a model from disk, until one couldn’t be found anymore, in which case we’ll return nil. Returning nil from an iterator’s next() method signals to Swift that the sequence has come to an end and the iteration will stop.

We initialize our iterator with a Database that we’ll use to load each model. We default to using a shared instance of the database, but to facilitate testing, we enable dependency injection of it as well.

That’s it! Now we have a lazily evaluated sequence, that loads each model ad-hoc when it’s needed 🎉. We can now easily use our sequence whenever we want to iterate over all models in our database. For example, we can now search our database without having to load all of its records up front:

The nice thing about the code above, is that as soon as we’ve found a match, we can simply exit out of the iteration by returning, preventing further database records from being loaded.

OK, time for the bonus round! As we’ve just seen, implementing your own custom sequences and iterators in Swift is quite easy. But for when you are really lazy, the standard library’s AnySequence type has a closure-based API that you can use to quickly implement simple sequences, like this:

I’ve personally started to use custom sequences in a lot of different situations in my code. I find it not only more performant in cases like above, but also easier to debug as you can simply step through your iteration code to find any issues.

What do you think? Do you find the ability to define sequences useful in Swift? Let me know, along with any questions, comments or feedback — either here on Medium or find me on Twitter @johnsundell.

Thanks for reading! 🚀