<A, B, X>(f: (input: A, key: string) => Result<B, X>): (
self: Trie<A>
) => Trie<B>
<A, B, X>(
self: Trie<A>,
f: (input: A, key: string) => Result<B, X>
): Trie<B>Maps over the entries of the Trie using the specified filter and keeps
only successful results.
Example (Filtering and mapping entries)
import { Equal, Result, 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 trieMapV = Trie.empty<number>().pipe(
Trie.insert("she", 2)
)
const trieMapK = Trie.empty<number>().pipe(
Trie.insert("shells", 0),
Trie.insert("sells", 1)
)
assert.equal(
Equal.equals(
Trie.filterMap(trie, (v) => v > 1 ? Result.succeed(v) : Result.failVoid),
trieMapV
),
true
)
assert.equal(
Equal.equals(
Trie.filterMap(
trie,
(v, k) => k.length > 3 ? Result.succeed(v) : Result.failVoid
),
trieMapK
),
true
)export const const filterMap: {
<A, B, X>(
f: (input: A, key: string) => Result<B, X>
): (self: Trie<A>) => Trie<B>
<A, B, X>(
self: Trie<A>,
f: (input: A, key: string) => Result<B, X>
): Trie<B>
}
Maps over the entries of the Trie using the specified filter and keeps
only successful results.
Example (Filtering and mapping entries)
import { Equal, Result, 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 trieMapV = Trie.empty<number>().pipe(
Trie.insert("she", 2)
)
const trieMapK = Trie.empty<number>().pipe(
Trie.insert("shells", 0),
Trie.insert("sells", 1)
)
assert.equal(
Equal.equals(
Trie.filterMap(trie, (v) => v > 1 ? Result.succeed(v) : Result.failVoid),
trieMapV
),
true
)
assert.equal(
Equal.equals(
Trie.filterMap(
trie,
(v, k) => k.length > 3 ? Result.succeed(v) : Result.failVoid
),
trieMapK
),
true
)
filterMap: {
<function (type parameter) A in <A, B, X>(f: (input: A, key: string) => Result<B, X>): (self: Trie<A>) => Trie<B>A, function (type parameter) B in <A, B, X>(f: (input: A, key: string) => Result<B, X>): (self: Trie<A>) => Trie<B>B, function (type parameter) X in <A, B, X>(f: (input: A, key: string) => Result<B, X>): (self: Trie<A>) => Trie<B>X>(f: (input: A, key: string) => Result<B, X>f: (input: Ainput: function (type parameter) A in <A, B, X>(f: (input: A, key: string) => Result<B, X>): (self: Trie<A>) => Trie<B>A, key: stringkey: string) => import ResultResult<function (type parameter) B in <A, B, X>(f: (input: A, key: string) => Result<B, X>): (self: Trie<A>) => Trie<B>B, function (type parameter) X in <A, B, X>(f: (input: A, key: string) => Result<B, X>): (self: Trie<A>) => Trie<B>X>): (self: Trie<A>(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) A in <A, B, X>(f: (input: A, key: string) => Result<B, X>): (self: Trie<A>) => Trie<B>A>) => 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) B in <A, B, X>(f: (input: A, key: string) => Result<B, X>): (self: Trie<A>) => Trie<B>B>
<function (type parameter) A in <A, B, X>(self: Trie<A>, f: (input: A, key: string) => Result<B, X>): Trie<B>A, function (type parameter) B in <A, B, X>(self: Trie<A>, f: (input: A, key: string) => Result<B, X>): Trie<B>B, function (type parameter) X in <A, B, X>(self: Trie<A>, f: (input: A, key: string) => Result<B, X>): Trie<B>X>(self: Trie<A>(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) A in <A, B, X>(self: Trie<A>, f: (input: A, key: string) => Result<B, X>): Trie<B>A>, f: (input: A, key: string) => Result<B, X>f: (input: Ainput: function (type parameter) A in <A, B, X>(self: Trie<A>, f: (input: A, key: string) => Result<B, X>): Trie<B>A, key: stringkey: string) => import ResultResult<function (type parameter) B in <A, B, X>(self: Trie<A>, f: (input: A, key: string) => Result<B, X>): Trie<B>B, function (type parameter) X in <A, B, X>(self: Trie<A>, f: (input: A, key: string) => Result<B, X>): Trie<B>X>): 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) B in <A, B, X>(self: Trie<A>, f: (input: A, key: string) => Result<B, X>): Trie<B>B>
} = import TRTR.const filterMap: anyfilterMap