NonEmpty

Episode #20 • Jun 25, 2018 • Subscriber-Only

We often deal with collections that we know can never be empty, yet we use arrays to model them. Using the ideas from our last episode on algebraic data types, we develop a NonEmpty type that can be used to transform any collection into a non-empty version of itself.

Previous episode
NonEmpty
Next episode
Locked

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Sign in with GitHub

Introduction

In our most recent episode on algebraic data types, we explored how we can use algebra to understand generics and recursive data structures in the Swift type system. Things got pretty abstract! We grounded the episode, though, in a useful type that doesn’t seem to get enough attention: the non-empty list—in fact, we gave it twice as much attention because math led us to two different formulas!

We deal with non-empty data regularly, but without a structure at hand it’s all too easy to reach for Array and inevitably we end up writing a bit of extra code to handle those empty cases. Today’s episode is all about embracing type-safe guarantees, avoiding those extra code paths, and building an ergonomic API over the non-empty list. Let’s build a low-friction, non-empty type suitable for your production code base!

Let’s start with what we came up with last time.

Recap


References

  • NonEmpty
    Brandon Williams & Stephen Celis • Jul 25, 2018

    NonEmpty is one of our open source projects for expressing a type safe, compiler proven non-empty collection of values.

  • Validated
    Brandon Williams & Stephen Celis • Aug 17, 2018

    Validated is one of our open source projects that provides a Result-like type, which supports a zip operation. This means you can combine multiple validated values into a single one and accumulate all of their errors.

Downloads

Sample code

Get started with our free plan

Our free plan includes 1 subscriber-only episode of your choice, access to 62 free episodes with transcripts and code samples, and weekly updates from our newsletter.

View plans and pricing