Skip to main content
Module

x/fun/optic.ts>traverse

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

Construct a composable optic from a Traversable instance for a Kind T. This will fold the values wrapped in the Kind T into a single Array when viewed.

Examples

Example 1

import * as O from "./optic.ts";
import * as T from "./tree.ts";
import { pipe } from "./fn.ts";

type Data = { tree: T.Tree<number> };

const numbers = pipe(
  O.id<Data>(),
  O.prop("tree"),
  O.traverse(T.TraversableTree),
);

const tree1: Data = { tree: T.tree(1, [T.tree(2), T.tree(3)]) };
const tree2: Data = { tree: T.tree(0) };

const result1 = pipe(numbers, O.view(tree1)); // [1, 2, 3]
const result2 = pipe(numbers, O.view(tree2)); // [0]
const result3 = pipe(tree1, numbers.modify(n => n + 1));
// Tree(2, [Tree(3), Tree(4)])

Returns

<U extends Tag, S, A, B, C, D, E>(first: Optic<U, S, $<T, [A, B, C], [D], [E]>>) => Optic<Align<U, FoldTag>, S, A>