Skip to main content
Module

x/fun/mod.ts>array.orderedInsert

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

Given an Sortable construct a curried insert function that inserts values into a new array in a sorted fashion. Internally this uses binarySearch to find the insertion index of any inserted items. Since the returned function will always insert this function will always return a new array.

Examples

Example 1

import * as A from "./array.ts";
import * as O from "./sortable.ts";
import { SortableNumber } from "./number.ts";
import { pipe } from "./fn.ts";

type Person = { name: string, age: number };
function person(name: string, age: number) {
  return { name, age };
}

const SortablePerson = pipe(
  SortableNumber,
  O.premap((p: Person) => p.age),
);
const insert = A.orderedInsert(SortablePerson);

const result = pipe(
  A.init(),
  insert(person("Brandon", 37)),
  insert(person("Emily", 32)),
  insert(
    person("Rufus", 0.7),
    person("Clementine", 0.5)
  ),
);
// [
// { name: "Clementine", age: 0.5 },
// { name: "Rufus", age: 0.7 },
// { name: "Emily", age: 32 },
// { name: "Brandon", age: 37 },
// ]

Parameters

ord: Sortable<A>

Returns

(...values: NonEmptyArray<A>) => (arr: ReadonlyArray<A>) => ReadonlyArray<A>