February 2018
Intermediate to advanced
350 pages
7h 35m
English
Optics are abstractions to update immutable data structures elegantly. One form of optics is Lens (or lenses, depending on the library implementation). A Lens is a functional reference that can focus (hence the name) into a structure and read, write, or modify its target:
typealias GB = Intdata class Memory(val size: GB)data class MotherBoard(val brand: String, val memory: Memory)data class Laptop(val price: Double, val motherBoard: MotherBoard)fun main(args: Array<String>) { val laptopX8 = Laptop(500.0, MotherBoard("X", Memory(8))) val laptopX16 = laptopX8.copy( price = 780.0, motherBoard = laptopX8.motherBoard.copy( memory = laptopX8.motherBoard.memory.copy( size = laptopX8.motherBoard.memory.size * 2 ) ) ) println("laptopX16 = ...
Read now
Unlock full access