a minimum Lens type, and code generation helper inspired by lens
A Lens is a functional common problem: how to easily update a complex immutable structure, for this purpose Lens acts as a zoom into a record.
example.
struct Street { let name: String }
struct Address { let street: Street }
struct Company { let address: Address }
struct Employee { let company: Company }
let employee = Employee(company: Company(address: Address(street: Street(name: "street"))))
use lens
let _name: Lens<Street, String> = Lens(get: { $0.name }, set: { Street(name: $1) })
let _street: Lens<Address, Street> = Lens(get: { $0.street }, set: { Address(street: $1) })
let _address: Lens<Company, Address> = Lens(get: { $0.address }, set: { Company(address: $1) })
let _company: Lens<Employee, Company> = Lens(get: { $0.company }, set: { Employee(company: $1) })
(_company >>> _address >>> _street >>> _name).modify(employee) { $0.capitalizedString } // Street
Code generation with Sourcery
By using Scripts/generate-auto-lens.sh, LensKit will support lens-implementation
to auto-generate extension YourClass: AutoLens
.
# Usage: ./generate-auto-lens.sh <source_dir> <code-generated-dir>
$ ./path/to/LensKit/Scripts/generate-auto-lens.sh ./Sources ./Sources/Genes/
output file: AutoLens.generated.swift
Add following line into your Cartfile and run carthage update
.
github "to4iki/LensKit"
Add following line into your Package.swift and run swift build
.
dependencies: [
.Package(url: "https://github.com/to4iki/LensKit.git", majorVersion: 0)
]