Advanced Swift Optics
summary
Explains how to use concrete optics in Swift to manipulate deeply nested value types: builds Lens from writable key paths, creates a dictionary-key lens and a zipped lens for simultaneous fields, defines Prism for enum cases with match/build and tryModify, and generalizes both as an Affine to target optional or indexed elements (first matching predicate in an array). Shows composing these optics to locate a transaction outcome through enum → dictionary → array → field and update its balance, with working Swift playground code and pattern examples.
AI-written summary · about
chapters (11)
- 0:00Intro
- 0:12Advanced Swift Optics
- 2:051. Total: Thay return an output for every Input. 2. Deterministic: They return the same output for the same input. 3. Pure: Their only effect is computing the output.
- 5:16Show me your flowcharts and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won't usually need your flowcharts; they'll be obvious.
- 6:13domain modeling • Ul modeling • transitional information • intent representation
- 10:35separation of representation from interpretation • the former at the core, the latter at the boundary • readable, testable, maintainable • not just for functional programming
- 12:32they can get messy and complex • lots of nesting • we usually operate on tiny parts... ...but we need to transform the whole
- 14:17types (usually structs) • encapsulate various kinds of relationships between data structures • for example, "focusing": • relationship between a data structure and
- 15:18different sources and different platforms use different names • "Profunctor Optics - Modular Data Accessors by Matthew Pickering, Jeremy Gibbons, and Nicolas Wu • "Don't Fear the Profunctor Optics! GitHub repo
- 16:34like KeyPath (not a coincidence) • simplified representation • full representations has 2 more parameters: • generic change on both Root and Value
- 36:13Functional Programming rocks • even if just a bit • Well-defined data structures are the key to dear, testable and maintainable code • Optics: a useful tool to operate on data structures in a generic way • Lens: structs and classes
related talks
40mFunctional iOS Architecture in the SwiftUI Era
57mLet's Lens - Part 2 of 5
36mLenses for the Masses: Introducing Goggles
39mSwift and You
31mMaking the Most Out of Swift
32mFunctional Lenses in JavaScript
47mExploring Key Practices of Observation and SwiftData in New Application Development
48mLearning From Our Elders: Applying functional insights without losing Swift
