Skip to content

to4iki/LensKit

Repository files navigation

LensKit

Swift 4.2 Build Status Carthage compatible

a minimum Lens type, and code generation helper inspired by lens

Overview

Monocle.Lens - elm-monocle

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

Installation

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)
]