Hyperlinkv0.8.0-beta.28

Graph

Graph.mapNodesconsteffect/Graph.ts:1018
<N, E, T extends Kind = "directed">(
  mutable: MutableGraph<N, E, T>,
  f: (data: N) => N
): void

Transforms every node's data in a mutable graph in place using the provided mapping function.

Details

Node indices and edges are preserved; only the stored node data is replaced.

Example (Mapping node data)

import { Graph } from "effect"

const graph = Graph.directed<string, number>((mutable) => {
  Graph.addNode(mutable, "node a")
  Graph.addNode(mutable, "node b")
  Graph.addNode(mutable, "node c")
  Graph.mapNodes(mutable, (data) => data.toUpperCase())
})

const nodeData = Graph.getNode(graph, 0)
console.log(nodeData) // Option.some("NODE A")
transforming
Source effect/Graph.ts:101810 lines
export const mapNodes = <N, E, T extends Kind = "directed">(
  mutable: MutableGraph<N, E, T>,
  f: (data: N) => N
): void => {
  // Transform existing node data in place
  for (const [index, data] of mutable.nodes) {
    const newData = f(data)
    mutable.nodes.set(index, newData)
  }
}