Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/fun/mod.ts>optics.traverse

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 optics.traverse
import { optics } from "https://deno.land/x/fun@v.2.0.0-alpha.11/mod.ts";
const { traverse } = optics;

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

Examples

Example 1

import * as O from "./optics.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)])

Type Parameters

T extends Kind

Parameters

T: Traversable<T>

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>