<N, E, T extends Kind = "directed">(
mutable: MutableGraph<N, E, T>,
f: (data: N) => Option.Option<N>
): voidFilters and optionally transforms nodes in a mutable graph using a predicate function. Nodes that return Option.none are removed along with all their connected edges.
Example (Filtering and mapping nodes)
import { Graph, Option } from "effect"
const graph = Graph.directed<string, number>((mutable) => {
const a = Graph.addNode(mutable, "active")
const b = Graph.addNode(mutable, "inactive")
const c = Graph.addNode(mutable, "active")
Graph.addEdge(mutable, a, b, 1)
Graph.addEdge(mutable, b, c, 2)
// Keep only "active" nodes and transform to uppercase
Graph.filterMapNodes(
mutable,
(data) =>
data === "active" ? Option.some(data.toUpperCase()) : Option.none()
)
})
console.log(Graph.nodeCount(graph)) // 2 (only "active" nodes remain)export const const filterMapNodes: <
N,
E,
T extends Kind = "directed"
>(
mutable: MutableGraph<N, E, T>,
f: (data: N) => Option.Option<N>
) => void
Filters and optionally transforms nodes in a mutable graph using a predicate function.
Nodes that return Option.none are removed along with all their connected edges.
Example (Filtering and mapping nodes)
import { Graph, Option } from "effect"
const graph = Graph.directed<string, number>((mutable) => {
const a = Graph.addNode(mutable, "active")
const b = Graph.addNode(mutable, "inactive")
const c = Graph.addNode(mutable, "active")
Graph.addEdge(mutable, a, b, 1)
Graph.addEdge(mutable, b, c, 2)
// Keep only "active" nodes and transform to uppercase
Graph.filterMapNodes(
mutable,
(data) =>
data === "active" ? Option.some(data.toUpperCase()) : Option.none()
)
})
console.log(Graph.nodeCount(graph)) // 2 (only "active" nodes remain)
filterMapNodes = <function (type parameter) N in <N, E, T extends Kind = "directed">(mutable: MutableGraph<N, E, T>, f: (data: N) => Option.Option<N>): voidN, function (type parameter) E in <N, E, T extends Kind = "directed">(mutable: MutableGraph<N, E, T>, f: (data: N) => Option.Option<N>): voidE, function (type parameter) T in <N, E, T extends Kind = "directed">(mutable: MutableGraph<N, E, T>, f: (data: N) => Option.Option<N>): voidT extends type Kind = "directed" | "undirected"Graph type for distinguishing directed and undirected graphs.
When to use
Use when writing graph-polymorphic types or helpers that need to preserve
whether a graph is directed or undirected.
Kind = "directed">(
mutable: MutableGraph<N, E, T>(parameter) mutable: {
type: T;
mutable: true;
nodes: Map<NodeIndex, N>;
edges: Map<EdgeIndex, Edge<E>>;
adjacency: Map<NodeIndex, Array<EdgeIndex>>;
reverseAdjacency: Map<NodeIndex, Array<EdgeIndex>>;
nextNodeIndex: NodeIndex;
nextEdgeIndex: EdgeIndex;
acyclic: Option.Option<boolean>;
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;
}
mutable: interface MutableGraph<out N, out E, T extends Kind = "directed">Mutable graph interface.
When to use
Use when adding, removing, or updating nodes and edges inside a graph
mutation scope.
MutableGraph<function (type parameter) N in <N, E, T extends Kind = "directed">(mutable: MutableGraph<N, E, T>, f: (data: N) => Option.Option<N>): voidN, function (type parameter) E in <N, E, T extends Kind = "directed">(mutable: MutableGraph<N, E, T>, f: (data: N) => Option.Option<N>): voidE, function (type parameter) T in <N, E, T extends Kind = "directed">(mutable: MutableGraph<N, E, T>, f: (data: N) => Option.Option<N>): voidT>,
f: (data: N) => Option.Option<N>f: (data: Ndata: function (type parameter) N in <N, E, T extends Kind = "directed">(mutable: MutableGraph<N, E, T>, f: (data: N) => Option.Option<N>): voidN) => import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) N in <N, E, T extends Kind = "directed">(mutable: MutableGraph<N, E, T>, f: (data: N) => Option.Option<N>): voidN>
): void => {
const const nodesToRemove: Array<NodeIndex>nodesToRemove: interface Array<T>Array<type NodeIndex = numberNode index for node identification using plain numbers.
When to use
Use when storing or passing the stable identifier of a graph node between
Graph operations.
Details
addNode allocates node identifiers from the graph's next node index.
Gotchas
A NodeIndex is an identifier, not an array offset. Removed node identifiers
are not reused.
NodeIndex> = []
// First pass: identify nodes to remove and transform data for nodes to keep
for (const [const index: numberindex, const data: Ndata] of mutable: MutableGraph<N, E, T>(parameter) mutable: {
type: T;
mutable: true;
nodes: Map<NodeIndex, N>;
edges: Map<EdgeIndex, Edge<E>>;
adjacency: Map<NodeIndex, Array<EdgeIndex>>;
reverseAdjacency: Map<NodeIndex, Array<EdgeIndex>>;
nextNodeIndex: NodeIndex;
nextEdgeIndex: EdgeIndex;
acyclic: Option.Option<boolean>;
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;
}
mutable.Proto<N, E>.nodes: Map<NodeIndex, N>nodes) {
const const result: Option.Option<N>result = f: (data: N) => Option.Option<N>f(const data: Ndata)
if (import OptionOption.const isSome: <A>(
self: Option<A>
) => self is Some<A>
Checks whether an Option contains a value (Some).
When to use
Use when you need to branch on a present Option before accessing .value.
Details
- Acts as a type guard, narrowing to
Some<A>
Example (Checking for Some)
import { Option } from "effect"
console.log(Option.isSome(Option.some(1)))
// Output: true
console.log(Option.isSome(Option.none()))
// Output: false
isSome(const result: Option.Option<N>result)) {
// Transform node data
mutable: MutableGraph<N, E, T>(parameter) mutable: {
type: T;
mutable: true;
nodes: Map<NodeIndex, N>;
edges: Map<EdgeIndex, Edge<E>>;
adjacency: Map<NodeIndex, Array<EdgeIndex>>;
reverseAdjacency: Map<NodeIndex, Array<EdgeIndex>>;
nextNodeIndex: NodeIndex;
nextEdgeIndex: EdgeIndex;
acyclic: Option.Option<boolean>;
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;
}
mutable.Proto<N, E>.nodes: Map<NodeIndex, N>nodes.Map<number, N>.set(key: number, value: N): Map<number, N>Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.
set(const index: numberindex, const result: Option.Some<N>const result: {
_tag: "Some";
_op: "Some";
value: A;
valueOrUndefined: A;
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;
}
result.Some<N>.value: Nvalue)
} else {
// Mark for removal
const nodesToRemove: Array<NodeIndex>nodesToRemove.Array<number>.push(...items: number[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(const index: numberindex)
}
}
// Second pass: remove filtered out nodes and their edges
for (const const nodeIndex: numbernodeIndex of const nodesToRemove: Array<NodeIndex>nodesToRemove) {
const removeNode: <
N,
E,
T extends Kind = "directed"
>(
mutable: MutableGraph<N, E, T>,
nodeIndex: NodeIndex
) => void
Removes a node and all its incident edges from a mutable graph.
Example (Removing a node)
import { Graph } from "effect"
const result = Graph.mutate(Graph.directed<string, number>(), (mutable) => {
const nodeA = Graph.addNode(mutable, "Node A")
const nodeB = Graph.addNode(mutable, "Node B")
Graph.addEdge(mutable, nodeA, nodeB, 42)
// Remove nodeA and all edges connected to it
Graph.removeNode(mutable, nodeA)
})
removeNode(mutable: MutableGraph<N, E, T>(parameter) mutable: {
type: T;
mutable: true;
nodes: Map<NodeIndex, N>;
edges: Map<EdgeIndex, Edge<E>>;
adjacency: Map<NodeIndex, Array<EdgeIndex>>;
reverseAdjacency: Map<NodeIndex, Array<EdgeIndex>>;
nextNodeIndex: NodeIndex;
nextEdgeIndex: EdgeIndex;
acyclic: Option.Option<boolean>;
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;
}
mutable, const nodeIndex: numbernodeIndex)
}
}