MCPcopy Create free account
hub / github.com/Effect-TS/effect / filterMapNodes

Function filterMapNodes

packages/effect/src/Graph.ts:2027–2052  ·  view source on GitHub ↗
(
  mutable: MutableGraph<N, E, T>,
  f: (data: N) => Option.Option<N>
)

Source from the content-addressed store, hash-verified

2025 * @since 3.18.0
2026 */
2027export const filterMapNodes = <N, E, T extends Kind = "directed">(
2028 mutable: MutableGraph<N, E, T>,
2029 f: (data: N) => Option.Option<N>
2030): void => {
2031 assertMutable(mutable)
2032 const impl = graphImpl(mutable)
2033
2034 const nodesToRemove: Array<NodeIndex> = []
2035
2036 // First pass: identify nodes to remove and transform data for nodes to keep
2037 for (const [index, data] of impl.nodes) {
2038 const result = f(data)
2039 if (Option.isSome(result)) {
2040 // Transform node data
2041 impl.nodes.set(index, result.value)
2042 } else {
2043 // Mark for removal
2044 nodesToRemove.push(index)
2045 }
2046 }
2047
2048 // Second pass: remove filtered out nodes and their edges
2049 for (const nodeIndex of nodesToRemove) {
2050 removeNode(mutable, nodeIndex)
2051 }
2052}
2053
2054/**
2055 * Filters and optionally transforms edges in a mutable graph using a predicate function.

Callers

nothing calls this directly

Calls 6

assertMutableFunction · 0.85
graphImplFunction · 0.85
removeNodeFunction · 0.85
pushMethod · 0.80
setMethod · 0.65
fFunction · 0.50

Tested by

no test coverage detected