The Many Faces of Zip: Part 2

Episode #24 • Jul 30, 2018 • Subscriber-Only

In part two of our series on zip we will show that many types support a zip-like operation, and some even support multiple distinct implementations. However, not all zips are created equal, and understanding this can lead to some illuminating properties of our types.

Previous episode
The Many Faces of Zip: Part 2
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 the last episode we began the journey into exploring the “many faces of zip”. First we played around with the zip that the standard library gives us a bit and saw that it was really useful for coordinating the elements of two sequences in a very safe way so that we don’t have to juggle indices.

Then we zoomed out a bit and looked at the true signature of zip, and we saw that what it was really doing was kinda flipping containers: it transformed a tuple of arrays into an array of tuples.

After that we saw that zip is just a generalization of map: where map allows us to transform a function (A) -> B into a function ([A]) -> [B], zip allowed us to transform a function (A, B) -> C into a function ([A], [B]) -> [C].

This empowered us to define zip on optionals, which is not something that people typically do. It ended up being the perfect solution to a problem that plagued Swift 1: nested if lets! Swift 2 fixed this, but zip still can provide a more ergonomic solution than multiple if lets on the same line.

Zip on other types: Result


References

  • 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 64 free episodes with transcripts and code samples, and weekly updates from our newsletter.

View plans and pricing