Optics in fp-ts 🧐

What is a lens?

A lens is a function that takes a key and returns a set of functions you can use to handle data at that specified key (or any nested data structure)

 

These modifiers return back a fresh copy of the entire state with the key's new value

 

Lenses can be composed together to peer deeper into state and update it

What is a lens in fp-ts?

import { Lens } from "monocle-ts";

type Hobbies = string[];

type Details = {
  hobbies: Hobbies;
};

type Person = {
  name: string;
  details: Details;
};

const adam: Person = {
  name: "Adam",
  details: {
    hobbies: ["reasonml", "typescript", "purescript"]
  }
};

const nameLens = Lens.fromProp<Person>()("name");

export const name = nameLens.get(adam);

The End

Optics in fp-ts

By Adam Recvlohe

Optics in fp-ts

  • 978