From 84aec25d420f4e623cf2a334b4f9b5ce39cd64e2 Mon Sep 17 00:00:00 2001 From: Sebastian Lorenz Date: Tue, 4 Nov 2025 10:52:22 +0100 Subject: [PATCH] add more `dual` variants for Graph module (#709) --- packages/effect/src/collections/Graph.ts | 105 +++++++++++++++++++---- 1 file changed, 87 insertions(+), 18 deletions(-) diff --git a/packages/effect/src/collections/Graph.ts b/packages/effect/src/collections/Graph.ts index 91c176c6f2..6625b9891a 100644 --- a/packages/effect/src/collections/Graph.ts +++ b/packages/effect/src/collections/Graph.ts @@ -2682,7 +2682,15 @@ export interface DijkstraConfig { * @since 4.0.0 * @category algorithms */ -export const dijkstra = ( +export const dijkstra: { + ( + config: DijkstraConfig + ): (graph: Graph | MutableGraph) => PathResult | undefined + ( + graph: Graph | MutableGraph, + config: DijkstraConfig + ): PathResult | undefined +} = dual(2, ( graph: Graph | MutableGraph, config: DijkstraConfig ): PathResult | undefined => { @@ -2806,7 +2814,7 @@ export const dijkstra = ( distance, costs } -} +}) /** * Result of all-pairs shortest path computation. @@ -2849,7 +2857,15 @@ export interface AllPairsResult { * @since 4.0.0 * @category algorithms */ -export const floydWarshall = ( +export const floydWarshall: { + ( + cost: (edgeData: E) => number + ): (graph: Graph | MutableGraph) => AllPairsResult + ( + graph: Graph | MutableGraph, + cost: (edgeData: E) => number + ): AllPairsResult +} = dual(2, ( graph: Graph | MutableGraph, cost: (edgeData: E) => number ): AllPairsResult => { @@ -2958,7 +2974,7 @@ export const floydWarshall = ( paths, costs } -} +}) /** * Configuration for A* pathfinding algorithm. @@ -3012,7 +3028,15 @@ export interface AstarConfig { * @since 4.0.0 * @category algorithms */ -export const astar = ( +export const astar: { + ( + config: AstarConfig + ): (graph: Graph | MutableGraph) => PathResult | undefined + ( + graph: Graph | MutableGraph, + config: AstarConfig + ): PathResult | undefined +} = dual(2, ( graph: Graph | MutableGraph, config: AstarConfig ): PathResult | undefined => { @@ -3160,7 +3184,7 @@ export const astar = ( distance, costs } -} +}) /** * Configuration for Bellman-Ford algorithm. @@ -3209,7 +3233,15 @@ export interface BellmanFordConfig { * @since 4.0.0 * @category algorithms */ -export const bellmanFord = ( +export const bellmanFord: { + ( + config: BellmanFordConfig + ): (graph: Graph | MutableGraph) => PathResult | undefined + ( + graph: Graph | MutableGraph, + config: BellmanFordConfig + ): PathResult | undefined +} = dual(2, ( graph: Graph | MutableGraph, config: BellmanFordConfig ): PathResult | undefined => { @@ -3336,7 +3368,7 @@ export const bellmanFord = ( distance, costs } -} +}) /** * Concrete class for iterables that produce [NodeIndex, NodeData] tuples. @@ -3579,7 +3611,15 @@ export interface SearchConfig { * @since 4.0.0 * @category iterators */ -export const dfs = ( +export const dfs: { + ( + config?: SearchConfig + ): (graph: Graph | MutableGraph) => NodeWalker + ( + graph: Graph | MutableGraph, + config?: SearchConfig + ): NodeWalker +} = dual((args) => isGraph(args[0]), ( graph: Graph | MutableGraph, config: SearchConfig = {} ): NodeWalker => { @@ -3630,7 +3670,7 @@ export const dfs = ( return { next: nextMapped } } })) -} +}) /** * Creates a new BFS iterator with optional configuration. @@ -3664,7 +3704,15 @@ export const dfs = ( * @since 4.0.0 * @category iterators */ -export const bfs = ( +export const bfs: { + ( + config?: SearchConfig + ): (graph: Graph | MutableGraph) => NodeWalker + ( + graph: Graph | MutableGraph, + config?: SearchConfig + ): NodeWalker +} = dual((args) => isGraph(args[0]), ( graph: Graph | MutableGraph, config: SearchConfig = {} ): NodeWalker => { @@ -3711,7 +3759,7 @@ export const bfs = ( return { next: nextMapped } } })) -} +}) /** * Configuration options for topological sort iterator. @@ -3768,7 +3816,12 @@ export interface TopoConfig { * @since 4.0.0 * @category iterators */ -export const topo = ( +export const topo: { + ( + config?: TopoConfig + ): (graph: Graph | MutableGraph) => NodeWalker + (graph: Graph | MutableGraph, config?: TopoConfig): NodeWalker +} = dual((args) => isGraph(args[0]), ( graph: Graph | MutableGraph, config: TopoConfig = {} ): NodeWalker => { @@ -3849,7 +3902,7 @@ export const topo = ( return { next: nextMapped } } })) -} +}) /** * Creates a new DFS postorder iterator with optional configuration. @@ -3880,7 +3933,15 @@ export const topo = ( * @since 4.0.0 * @category iterators */ -export const dfsPostOrder = ( +export const dfsPostOrder: { + ( + config?: SearchConfig + ): (graph: Graph | MutableGraph) => NodeWalker + ( + graph: Graph | MutableGraph, + config?: SearchConfig + ): NodeWalker +} = dual((args) => isGraph(args[0]), ( graph: Graph | MutableGraph, config: SearchConfig = {} ): NodeWalker => { @@ -3945,7 +4006,7 @@ export const dfsPostOrder = ( return { next: nextMapped } } })) -} +}) /** * Creates an iterator over all node indices in the graph. @@ -4081,7 +4142,15 @@ export interface ExternalsConfig { * @since 4.0.0 * @category iterators */ -export const externals = ( +export const externals: { + ( + config?: ExternalsConfig + ): (graph: Graph | MutableGraph) => NodeWalker + ( + graph: Graph | MutableGraph, + config?: ExternalsConfig + ): NodeWalker +} = dual((args) => isGraph(args[0]), ( graph: Graph | MutableGraph, config: ExternalsConfig = {} ): NodeWalker => { @@ -4115,4 +4184,4 @@ export const externals = ( return { next: nextMapped } } })) -} +})