Skip to main content
Module

x/fun/array.ts>binarySearch

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

Given an Sortable over A, create a binary search function for a sorted ReadonlyArray that returns the array index that the new value should be inserted at in order to maintain a sorted array.

Examples

Example 1

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

const search = A.binarySearch(SortableNumber);
const arr = A.range(100, 1); // [1, 2, ..., 100]

const index1 = search(30.5, arr); // Index 29
const index2 = search(10000, arr); // Index 100

Parameters

unnamed 0: Sortable<A>

Returns

(value: A, sorted: ReadonlyArray<A>) => number