O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Copy constructors and lenses

After examining our immutable example implementation, we are not able to say that it covers all the functionalities of the imperative approach. For instance, it does not provide us with a way to change the producer of a product. After all, we cannot change it.

Whenever we need to change any property of the product, we need to go through the following process:

let mexBananas = FunctionalProduct(name: bananas.name,                                    price: bananas.price,                                    quantity: bananas.quantity,                                    producer: Producer(name: "XYZ",                                                       address: "New                                                       Mexico, Mexico")) 

This solution is verbose and does not look nice. Let's examine how we can improve this process.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required