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

Decodable Randomness: Part 1

This week we dive deeper into randomness and composition by looking to a seemingly random place: the Decodable protocol. While we’re used to using the Codable set of protocols when working with JSON serialization and deserialization, it opens the opportunity for so much more.

#31 • Monday Sep 24, 2018 • Subscriber-only

Decodable Randomness: Part 1

This week we dive deeper into randomness and composition by looking to a seemingly random place: the Decodable protocol. While we’re used to using the Codable set of protocols when working with JSON serialization and deserialization, it opens the opportunity for so much more.


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. We skipped over the allKeys property of the KeyedDecodingContainerProtocol, but it’s what’s necessary to decode dictionaries of values. On initialization of the KeyedDecodingContainer, generate a random number of random CodingKeys to populate this property.

    You’ll need to return true from contains(_ key: Key).

    Decode a few random dictionaries of various decodable keys and values. What are some of the limitations of decoding dictionaries?

  2. Create a new UnkeyedContainer struct that conforms to the UnkeyedContainerProtocol and return it from the unkeyedContainer() method of ArbitraryDecoder. As with the KeyedDecodingContainer, you can delete the same decode methods and have them delegate to the SingleValueContainer.

    The count property can be used to generate a randomly-sized container, while currentIndex and isAtEnd can be used to let the decoder know how far along it is. Generate a random count, default the currentIndex to 0, and define isAtEnd as a computed property using these values. The currentIndex property should increment whenever superDecoder is called.

    Decode a few random arrays of various decodable elements.

Chapters
Introduction
00:06
An arbitrary decoder
01:43
Decoding single values
05:15
Decoding more structure
14:18
Till next time…
20:13
Credits

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