Skip to main content
Module

x/fun/record.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/record.ts";

Traverse a ReadonlyRecord, mapping each A into an algebraic data type V (so V), then collecting each I in V back into a ReadonlyRecord, ultimately returning V<ReadonlyRecord>. In more concrete terms this can take ReadonlyRecord<Option> and return Option<ReadonlyRecord> (or any other inner type.

Traverse, in general, is much like reducing and collecting over the outer and inner types of an ADT at the same time.

Examples

Example 1

import * as R from "./record.ts";
import * as O from "./option.ts";
import { pipe } from "./fn.ts";

const traverseOption = R.traverse(O.ApplicableOption);
const swapOption = traverseOption((o: O.Option<number>) => o);

const result1 = swapOption({ one: O.some(1), two: O.some(2) });
// Some({ one: 1, two: 2 });
const result2 = swapOption({ one: O.some(1), two: O.none }); // None

TODO: Revisit because mutability is bad here

Returns

<A, I, J = never, K = never, L = unknown, M = unknown>(favi: (value: A, key: string) => $<V, [I, J, K], [L], [M]>) => (ua: ReadonlyRecord<A>) => $<V, [ReadonlyRecord<I>, J, K], [L], [M]>