April 2017
Intermediate to advanced
316 pages
9h 33m
English
In the previous section, we covered copy constructors. Here, we will examine a functional structure called a lens. Simply put, lenses are functional getters and setters that are implemented for a whole object and its parts:
Let's implement a lens:
struct Lens<Whole, Part> { let get: (Whole) -> Part let set: (Part, Whole) -> Whole }
Let's use it to change our FunctionalProduct object to get and set the producer property:
let prodProducerLens: Lens<FunctionalProduct, Producer> = Lens(get: { $0.producer }, set: { FunctionalProduct(name: $1.name, price: $1.price, quantity: $1.quantity, ...Read now
Unlock full access