Functional Setters

Episode #6 • Mar 5, 2018 • Subscriber-Only

The programs we write can be reduced to transforming data from one form into another. We’re used to transforming this data imperatively, with setters. There’s a strange world of composition hiding here in plain sight, and it has a surprising link to a familiar functional friend.

Previous episode
Functional Setters
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

Today let’s talk about setters! So, the problem is that in our applications we often come across complicated, deeply nested data structures, and we want to be able to modify parts of those structures while keeping everything else fixed. Further, we wanna be able to do it a simple, clean, composable way, where “composable” means that if we have two ways of modify parts of a structure, I should be able to combine them into one thing that modifies both of those parts at the same time.

Tuples

Let’s look at something simple enough:

let pair = (42, "Swift")

References

  • Composable Setters
    Stephen Celis • Sep 30, 2017

    Stephen spoke about functional setters at the Functional Swift Conference if you’re looking for more material on the topic to reinforce the ideas.

  • Semantic editor combinators
    Conal Elliott • Nov 24, 2008

    Conal Elliott describes the setter composition we explored in this episode from first principles, using Haskell. In Haskell, the backwards composition operator <<< is written simply as a dot ., which means that g . f is the composition of two functions where you apply f first and then g. This means if had a nested value of type ([(A, B)], C) and wanted to create a setter that transform the B part, you would simply write it as first.map.second, and that looks eerily similar to how you would field access in the OOP style!

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