Skip to content

Instantly share code, notes, and snippets.

View johnthecat's full-sized avatar

Sergey Zhuravlev johnthecat

View GitHub Profile
@johnthecat
johnthecat / 1_lens.ts
Last active September 10, 2023 20:12
Simplified functor-less implementation of Van Laarhoven lenses in Typescript.
export type Lens<S, A> = (f: (y: A) => A, x: S) => S;
export const view = <S, A>(lens: Lens<S, A>) => (x: S) => {
let value: unknown = null;
lens(x => (value = x), x);
// Because of lack of functor we can't create beautiful composition of .map.map... etc.,
// so Const functor here is just plain variable without initializer
return value as A;
};