Skip to main content
Module

x/fun/mod.ts>optic.atMap

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Latest
function optic.atMap
import { optic } from "https://deno.land/x/fun@v2.0.0/mod.ts";
const { atMap } = optic;

Construct a composable combinator from an instance of Comparable 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 "./optic.ts";
import * as M from "./map.ts";
import { ComparableString, toLowerCase } from "./string.ts";
import { premap } from "./comparable.ts";
import { constNone } from "./option.ts";
import { pipe } from "./fn.ts";

type Words = ReadonlyMap<string, number>;

const insensitive = pipe(ComparableString, premap(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.init())); // None
const result3 = remove(new Map([["FUN", 100], ["not", 10]]));
// Map("not": 10);

Parameters

eq: Comparable<B>

Returns

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