Skip to main content
Module

x/fun/optics.ts>atMap

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Go to Latest
function atMap
import { atMap } from "https://deno.land/x/fun@v.2.0.0-alpha.11/optics.ts";

Construct a composable combinator from an instance of Eq and a key of a map. The combinator can then be composed with an existing optic to access or remove the value in the map.

Examples

Example 1

import * as O from "./optics.ts";
import * as M from "./map.ts";
import { EqString, toLowerCase } from "./string.ts";
import { contramap } from "./eq.ts";
import { constNone } from "./option.ts";
import { pipe } from "./fn.ts";

type Words = ReadonlyMap<string, number>;

const insensitive = pipe(EqString, contramap(toLowerCase));

const fun = pipe(O.id<Words>(), O.atMap(insensitive)("fun"));
const remove = pipe(fun, O.replace(constNone()));

const result1 = pipe(fun, O.view(new Map([["FUN", 100]]))); // Some(100)
const result2 = pipe(fun, O.view(M.empty())); // None
const result3 = remove(new Map([["FUN", 100], ["not", 10]]));
// Map("not": 10);

Returns

(key: B) => <U extends Tag, S, A>(first: Optic<U, S, ReadonlyMap<B, A>>) => Optic<Align<U, LensTag>, S, Option<A>>