<V>(entries: Iterable<readonly [string, V]>): Trie<V>Creates a new Trie from an iterable collection of key/value pairs (e.g. Array<[string, V]>).
Example (Creating a trie from entries)
import { Equal, Trie } from "effect"
import * as assert from "node:assert"
const iterable: Array<readonly [string, number]> = [["call", 0], ["me", 1], [
"mind",
2
], ["mid", 3]]
const trie = Trie.fromIterable(iterable)
// The entries in the `Trie` are extracted in alphabetical order, regardless of the insertion order
assert.deepStrictEqual(Array.from(trie), [["call", 0], ["me", 1], ["mid", 3], [
"mind",
2
]])
assert.equal(
Equal.equals(
Trie.make(["call", 0], ["me", 1], ["mind", 2], ["mid", 3]),
trie
),
true
)constructors
Source effect/Trie.ts:1221 lines
export const const fromIterable: <V>(
entries: Iterable<readonly [string, V]>
) => Trie<V>
Creates a new Trie from an iterable collection of key/value pairs (e.g. Array<[string, V]>).
Example (Creating a trie from entries)
import { Equal, Trie } from "effect"
import * as assert from "node:assert"
const iterable: Array<readonly [string, number]> = [["call", 0], ["me", 1], [
"mind",
2
], ["mid", 3]]
const trie = Trie.fromIterable(iterable)
// The entries in the `Trie` are extracted in alphabetical order, regardless of the insertion order
assert.deepStrictEqual(Array.from(trie), [["call", 0], ["me", 1], ["mid", 3], [
"mind",
2
]])
assert.equal(
Equal.equals(
Trie.make(["call", 0], ["me", 1], ["mind", 2], ["mid", 3]),
trie
),
true
)
fromIterable: <function (type parameter) V in <V>(entries: Iterable<readonly [string, V]>): Trie<V>V>(entries: Iterable<readonly [string, V]>entries: interface Iterable<T, TReturn = any, TNext = any>Iterable<readonly [string, function (type parameter) V in <V>(entries: Iterable<readonly [string, V]>): Trie<V>V]>) => interface Trie<in out Value>An immutable string-keyed map optimized for prefix lookup. Iteration yields
[key, value] pairs in key order, and update operations such as insert and
remove return new Trie values.
Example (Using a trie for prefix search)
import { Trie } from "effect"
// Create a trie with string-to-number mappings
const trie: Trie.Trie<number> = Trie.make(
["apple", 1],
["app", 2],
["application", 3],
["banana", 4]
)
// Get values by exact key
console.log(Trie.get(trie, "apple")) // Some(1)
console.log(Trie.get(trie, "grape")) // None
// Find all keys with a prefix
console.log(Array.from(Trie.keysWithPrefix(trie, "app")))
// ["app", "apple", "application"]
// Iterate over all entries (sorted alphabetically)
for (const [key, value] of trie) {
console.log(`${key}: ${value}`)
}
// Output: "app: 2", "apple: 1", "application: 3", "banana: 4"
// Check if key exists
console.log(Trie.has(trie, "app")) // true
// Get size
console.log(Trie.size(trie)) // 4
Trie<function (type parameter) V in <V>(entries: Iterable<readonly [string, V]>): Trie<V>V> = import TRTR.const fromIterable: <V>(
entries: Iterable<readonly [string, V]>
) => Trie<V>
fromIterable