A new Swift video series exploring functional programming and more.
#24 • Monday Jul 30, 2018 • Subscriber-only

The Many Faces of Zip: Part 2

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.

#24 • Monday Jul 30, 2018 • Subscriber-only

The Many Faces of Zip: Part 2

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.


Subscribe to Point‑Free

This episode is for subscribers only. To access it, and all past and future episodes, become a subscriber today!

See subscription optionsorLog in

Sign up for our weekly newsletter to be notified of new episodes, and unlock access to any subscriber-only episode of your choosing!

Sign up for free episode


Exercises

  1. Can you make the zip2 function on our F3 type thread safe?

  2. Generalize the F3 type to a type that allows returning values other than Void: struct F4<A, R> { let run: (@escaping (A) -> R) -> R }. Define zip2 and zip2(with:) on the A type parameter.

  3. Find a function in the Swift standard library that resembles the function above. How could you use zip2 on it?

  4. This exercise explore what happens when you nest two types that each support a zip operation.

    • Consider the type [A]? = Optional<Array<A>>. The outer layer Optional has zip2 defined, but also the inner layer Array has a zip2. Can we define a zip2 on [A]? that makes use of both of these zip structures? Write the signature of such a function and implement it.
    • Using the zip2 defined above write an example usage of it involving two [A]? values.
    • Consider the type [Validated<A, E>]. We again have have a nesting of types, each of which have their own zip2 operation. Can you define a zip2 on this type that makes use of both zip structures? Write the signature of such a function and implement it.
    • Using the zip2 defined above write an example usage of it involving two [Validated<A, E>] values.
    • Consider the type Func<R, A?>. Again we have a nesting of types, each of which have their own zip2 operation. Can you define a zip2 on this type that makes use of both structures? Write the signature of such a function and implement it.
    • Consider the type Func<R, [A]>. Again we have a nesting of types, each of which have their own zip2 operation. Can you define a zip2 on this type that makes use of both structures? Write the signature of such a function and implement it.
    • Finally, conisder the type F3<Validated<A, E>>. Yet again we have a nesting of types, each of which have their own zip2 operation. Can you define a zip2 on this type that makes use of both structures? Write the signature of such a function and implement it.
  5. Do you see anything common in the implementation of all of the functions in the previous exercise? What this is showing is that nested zippable containers are also zippable containers because zip on the nesting can be defined in terms of zip on each of the containers.


References

  • Validated

    Brandon Williams & Stephen Celis • Friday 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.

Chapters
Introduction
00:05
Zip on other types: Result
01:38
Zip on other types: Validation
06:07
Zip on other types: Func
13:37
Zip on other types: F3
20:52
What’s the point?
34:44
Credits

Hosted by Brandon Williams and Stephen Celis. Recorded in Brooklyn, NY.