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

x/rimbu/proximity/common/index.ts>findNearestKeyMatch

Rimbu is a TypeScript library focused on immutable, performant, and type-safe collections and other tools.
Go to Latest
function findNearestKeyMatch
import { findNearestKeyMatch } from "https://deno.land/x/rimbu@1.1.0/proximity/common/index.ts";

Given an Iterable of [key, value] entries, applies the distance function to each key, finding the one closest to the input key, also returning its associated value as well as the related distance; returns undefined only when there are no successful match - that is, all the keys have +inf distance.

Performs a full linear scan unless a distance equal to 0 is encountered, in which case the function returns immediately; otherwise, the algorithm selects the smallest non-infinite distance: if multiple keys happen to have such distance, the selection order is not guaranteed.

Parameters

distanceFunction: DistanceFunction<K>

Returns the distance between two keys

key: K

The key used as a reference to find the closest key

entries: Iterable<readonly [K, V]>

The [key, value] entries

Returns

NearestKeyMatch<K, V> | undefined

A NearestKeyMatch object if a closest key - having a finite distance - exists; undefined otherwise