Swift.org Blog Nate Cook Oct 7, 2020

Swift Algorithms Library

Article Summary

Nate Cook from Apple's Swift team just dropped Swift Algorithms, an open-source package that could change how iOS developers write collection code. If you've ever written a gnarly nested loop to chunk data or find permutations, this is for you.

Apple released Swift Algorithms as an official open-source package containing battle-tested sequence and collection algorithms. Think Python's itertools or C++ algorithms library, but designed specifically for Swift's protocol-oriented approach. The package serves as a proving ground for algorithms before they graduate into the Swift standard library.

Key Takeaways

Critical Insight

Swift Algorithms gives iOS developers a curated toolkit of collection operations that improve code correctness while providing a faster path for new algorithms to reach the standard library.

The chunked(by:) examples alone will make you rethink how you've been processing arrays, and there are nine other algorithm families worth exploring.

About This Article

Problem

Apple struggled with the Swift Evolution process because proposals had to be small enough for meaningful discussion but large enough to justify adding new algorithms. The lengthy SE-0270 review made this tension clear.

Solution

Nate Cook's team built Swift Algorithms as a testing ground where they could explore different algorithm families and how they work together before adding them to the standard library. This complements the Evolution process the same way Swift Numerics does.

Impact

Apple now plans to split large algorithm families into smaller Evolution proposals. The Algorithms package gives them a place to work through ideas and handle big gaps in functionality in a more organized way.