🎉 End-of-year Sale! Save 25% when you subscribe today.

Decodable Randomness: Part 1

Episode #31 • Sep 24, 2018 • Subscriber-Only

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.

Previous episode
Decodable Randomness: Part 1
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

Last week we discussed the topic of “Composable Randomness”, which seeks to understand how randomness can be made more composable by using function composition. We also compared this with Swift 4.2’s new randomness API, which arguably is not composable, in the sense that it is not built from units that stand on their own and combine to form new units.

We spent some time building up fun generators, like randomly sized arrays of random values and even random password generators, both of which are not easily expressible with Swift’s APIs.

Today we are going to take this a big step forward, and unlock the ability to create random values from any data type we define. We’re actually going to do this in two different ways:

We’ll do a bunch of upfront work with a Swift specific feature that unlocks the ability to effortlessly generate random values from any data type, but we’ll see it’s a little bit rigid.

Then we’ll try another approach by doing a very modest amount of upfront work to build up the tools to generate random values from any data type in a very flexible way, but it takes a lil more work to do than the first method.

So let’s start with the first method, and it begins by look in an unlikely place: the Decodable Protocol.

An arbitrary decoder


Downloads

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