effect-io-ai

Package: effect
Module: Trie

Trie.longestPrefixOf

Returns the longest key/value in the Trie that is a prefix of that key if it exists, None otherwise.

Example (Finding the longest prefix)

import { Trie } from "effect"
import * as assert from "node:assert"

const trie = Trie.empty<number>().pipe(
  Trie.insert("shells", 0),
  Trie.insert("sells", 1),
  Trie.insert("she", 2)
)

const none = Trie.longestPrefixOf(trie, "sell")
const some = Trie.longestPrefixOf(trie, "sells")

assert.equal(none._tag, "None")
assert.equal(some._tag, "Some")
if (some._tag === "Some") {
  assert.deepStrictEqual(some.value, ["sells", 1])
}

Signature

declare const longestPrefixOf: { (key: string): <V>(self: Trie<V>) => Option<[string, V]>; <V>(self: Trie<V>, key: string): Option<[string, V]>; }

Source

Since v2.0.0