January 2019
Beginner
210 pages
4h 47m
English
We learned about lenses in some of the previous chapters. In one of the last examples, we implemented lenses in a way that is very close to the implementation provided by Ramda.
We implemented a higher-order function named lens that can be used to create a Lens implementation. The lens function takes two functions that must implement the Prop and Assoc interfaces:
interface Lens<T1, T2> { get(o: T1): T2; set(o: T2, v: T1): T1;}type Prop<T, K extends keyof T> = (o: T) => T[K];type Assoc<T, K extends keyof T> = (v: T[K], o: T) => T;const lens = <T1, K extends keyof T1>( getter: Prop<T1, K>, setter: Assoc<T1, K>,): Lens<T1, T1[K]> => { return { get: (obj: T1) => getter(obj), set: (val: T1[K], obj: T1) => setter(val, obj), };}const view ...Read now
Unlock full access