<Z, V>(zero: Z, f: (accumulator: Z, value: V, key: string) => Z): (
self: Trie<V>
) => Z
<Z, V>(
self: Trie<V>,
zero: Z,
f: (accumulator: Z, value: V, key: string) => Z
): ZReduces a state over the entries of the Trie.
Example (Reducing entries)
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)
)
assert.equal(
trie.pipe(
Trie.reduce(0, (acc, n) => acc + n)
),
3
)
assert.equal(
trie.pipe(
Trie.reduce(10, (acc, n) => acc + n)
),
13
)
assert.equal(
trie.pipe(
Trie.reduce("", (acc, _, key) => acc + key)
),
"sellssheshells"
)folding
Source effect/Trie.ts:6604 lines
export const const reduce: {
<Z, V>(
zero: Z,
f: (
accumulator: Z,
value: V,
key: string
) => Z
): (self: Trie<V>) => Z
<Z, V>(
self: Trie<V>,
zero: Z,
f: (
accumulator: Z,
value: V,
key: string
) => Z
): Z
}
Reduces a state over the entries of the Trie.
Example (Reducing entries)
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)
)
assert.equal(
trie.pipe(
Trie.reduce(0, (acc, n) => acc + n)
),
3
)
assert.equal(
trie.pipe(
Trie.reduce(10, (acc, n) => acc + n)
),
13
)
assert.equal(
trie.pipe(
Trie.reduce("", (acc, _, key) => acc + key)
),
"sellssheshells"
)
reduce: {
<function (type parameter) Z in <Z, V>(zero: Z, f: (accumulator: Z, value: V, key: string) => Z): (self: Trie<V>) => ZZ, function (type parameter) V in <Z, V>(zero: Z, f: (accumulator: Z, value: V, key: string) => Z): (self: Trie<V>) => ZV>(zero: Zzero: function (type parameter) Z in <Z, V>(zero: Z, f: (accumulator: Z, value: V, key: string) => Z): (self: Trie<V>) => ZZ, f: (
accumulator: Z,
value: V,
key: string
) => Z
f: (accumulator: Zaccumulator: function (type parameter) Z in <Z, V>(zero: Z, f: (accumulator: Z, value: V, key: string) => Z): (self: Trie<V>) => ZZ, value: Vvalue: function (type parameter) V in <Z, V>(zero: Z, f: (accumulator: Z, value: V, key: string) => Z): (self: Trie<V>) => ZV, key: stringkey: string) => function (type parameter) Z in <Z, V>(zero: Z, f: (accumulator: Z, value: V, key: string) => Z): (self: Trie<V>) => ZZ): (self: Trie<V>(parameter) self: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self: 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 <Z, V>(zero: Z, f: (accumulator: Z, value: V, key: string) => Z): (self: Trie<V>) => ZV>) => function (type parameter) Z in <Z, V>(zero: Z, f: (accumulator: Z, value: V, key: string) => Z): (self: Trie<V>) => ZZ
<function (type parameter) Z in <Z, V>(self: Trie<V>, zero: Z, f: (accumulator: Z, value: V, key: string) => Z): ZZ, function (type parameter) V in <Z, V>(self: Trie<V>, zero: Z, f: (accumulator: Z, value: V, key: string) => Z): ZV>(self: Trie<V>(parameter) self: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self: 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 <Z, V>(self: Trie<V>, zero: Z, f: (accumulator: Z, value: V, key: string) => Z): ZV>, zero: Zzero: function (type parameter) Z in <Z, V>(self: Trie<V>, zero: Z, f: (accumulator: Z, value: V, key: string) => Z): ZZ, f: (
accumulator: Z,
value: V,
key: string
) => Z
f: (accumulator: Zaccumulator: function (type parameter) Z in <Z, V>(self: Trie<V>, zero: Z, f: (accumulator: Z, value: V, key: string) => Z): ZZ, value: Vvalue: function (type parameter) V in <Z, V>(self: Trie<V>, zero: Z, f: (accumulator: Z, value: V, key: string) => Z): ZV, key: stringkey: string) => function (type parameter) Z in <Z, V>(self: Trie<V>, zero: Z, f: (accumulator: Z, value: V, key: string) => Z): ZZ): function (type parameter) Z in <Z, V>(self: Trie<V>, zero: Z, f: (accumulator: Z, value: V, key: string) => Z): ZZ
} = import TRTR.const reduce: anyreduce