Skip to Main Content
Hands-On Functional Programming with TypeScript
book

Hands-On Functional Programming with TypeScript

by Remo H. Jansen
January 2019
Beginner content levelBeginner
210 pages
4h 47m
English
Packt Publishing
Content preview from Hands-On Functional Programming with TypeScript

Lenses

A lens is just a pair of functions that allow us to get and set a value in an object. The interface of a lens could be declared as follows:

interface Lens<T1, T2> {    get(o: T1): T2;    set(o: T2, v: T1): T1;}

As we can see in the previous code snippet, the lens generic interface declares two methods. The get method can be used to get the value of a property of type T2 in an object of type T1. The set method can be used for the value of a property with type T2 in an object of type T1. The following code snippet implements the Lens interface: 

const streetLens: Lens<Address, Street> = {    get: (o: Address) => o.street,    set: (v: Street, o: Address) => new Address(o.city, v)};

The preceding implementation of the Lens interface is named streetLens ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Functional Programming in JavaScript

Functional Programming in JavaScript

Luis Atencio
TypeScript Quickly

TypeScript Quickly

Yakov Fain, Anton Moiseev
Functional Programming in Kotlin

Functional Programming in Kotlin

Runar Bjarnason, Paul Chiusano, Marco Vermeulen

Publisher Resources

ISBN: 9781788831437Supplemental Content