A new Swift video series exploring functional programming and more.
#6 • Monday Mar 5, 2018 • Subscriber-only

Functional Setters

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.

#6 • Monday Mar 5, 2018 • Subscriber-only

Functional Setters

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.


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

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.

Subscribe to Point-Free

👋 Hey there! Does this episode sound interesting? Well, then you may want to subscribe so that you get access to this episodes and more!


Exercises

  1. As we saw with free map on Array, define free map on Optional and use it to compose setters that traverse into an optional field.

  2. Take a struct, e.g.:

    struct User {
      let name: String
    }
    

    Write a setter for its property. Take (or add) another property, and add a setter for it. What are some potential issues with building these setters?

  3. Take a struct with a nested struct, e.g.:

    struct Location {
      let name: String
    }
    
    struct User {
      let location: Location
    }
    

    Write a setter for userLocationName. Now write setters for userLocation and locationName. How do these setters compose?

  4. Do first and second work with tuples of three or more values? Can we write first, second, third, and nth for tuples of n values?

  5. Write a setter for a dictionary that traverses into a key to set a value.

  6. Write a setter for a dictionary that traverses into a key to set a value if and only if that value already exists.

  7. What is the difference between a function of the form ((A) -> B) -> (C) -> (D) and one of the form (A) -> (B) -> (C) -> D?


References

  • Composable Setters

    Stephen Celis • Saturday 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 • Monday 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!

Chapters
Introduction
00:06
Tuples
00:31
Nested tuples
06:34
Introducing <<<
09:05
Arrays
13:33
What’s the point?
17:45