Skip to content

Commit d735abe

Browse files
committed
refactor(algorithms): convert path-planting functions to arrow functions
Convert path-planting functions from function declarations to arrow functions: - plantGroundTruthPaths (export) - signalStrengthToMI (internal) - pathId (internal) Also reorder imports to group type imports together and add JSDoc @param tags for better documentation.
1 parent a2e7b12 commit d735abe

File tree

3 files changed

+42
-51
lines changed

3 files changed

+42
-51
lines changed

packages/algorithms/src/evaluation/path-planting/heterogeneous-planting.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
* For graphs with multiple node types (e.g., OpenAlex with Works, Authors, Institutions)
55
*/
66

7-
import type { Edge, Node } from '../../types/graph';
87
import { Graph } from '../../graph/graph';
98
import type { Path } from '../../types/algorithm-results';
10-
import { plantGroundTruthPaths, type PlantedPathConfig } from './path-generator';
9+
import type { Edge, Node } from '../../types/graph';
10+
import { type PlantedPathConfig,plantGroundTruthPaths } from './path-generator';
1111

1212
/**
1313
* Extended configuration for heterogeneous graphs.
@@ -22,16 +22,17 @@ export interface HeterogeneousPathConfig<N extends Node, E extends Edge> extends
2222

2323
/**
2424
* Type-safe node type check.
25+
* @param node
2526
*/
26-
function getNodeType<N extends Node>(node: N): string {
27+
const getNodeType = <N extends Node>(node: N): string => {
2728
if ('type' in node && typeof node.type === 'string') {
2829
return node.type;
2930
}
3031
if ('entityType' in node && typeof node.entityType === 'string') {
3132
return node.entityType;
3233
}
3334
return 'unknown';
34-
}
35+
};
3536

3637
/**
3738
* Plant paths in heterogeneous graphs respecting entity type constraints.
@@ -45,11 +46,7 @@ function getNodeType<N extends Node>(node: N): string {
4546
* @param config - Planting configuration
4647
* @returns Graph with planted heterogeneous paths
4748
*/
48-
export function plantHeterogeneousPaths<N extends Node, E extends Edge>(
49-
graph: Graph<N, E>,
50-
pathTemplate: string[],
51-
config: HeterogeneousPathConfig<N, E>
52-
) {
49+
export const plantHeterogeneousPaths = <N extends Node, E extends Edge>(graph: Graph<N, E>, pathTemplate: string[], config: HeterogeneousPathConfig<N, E>) => {
5350
// Validate path template
5451
if (pathTemplate.length < 2) {
5552
throw new Error('Path template must have at least 2 node types');
@@ -91,22 +88,21 @@ export function plantHeterogeneousPaths<N extends Node, E extends Edge>(
9188
// Note: The path generator doesn't enforce intermediate node types,
9289
// so this is a best-effort implementation
9390
return plantGroundTruthPaths(graph, extendedConfig);
94-
}
91+
};
9592

9693
/**
9794
* Filter nodes by entity type.
95+
* @param nodes
96+
* @param entityType
9897
*/
99-
export function filterNodesByType<N extends Node>(nodes: N[], entityType: string): N[] {
100-
return nodes.filter(node => getNodeType(node) === entityType);
101-
}
98+
export const filterNodesByType = <N extends Node>(nodes: N[], entityType: string): N[] => nodes.filter(node => getNodeType(node) === entityType);
10299

103100
/**
104101
* Check if a path follows a type template.
102+
* @param path
103+
* @param template
105104
*/
106-
export function pathFollowsTemplate<N extends Node, E extends Edge>(
107-
path: Path<N, E>,
108-
template: string[]
109-
): boolean {
105+
export const pathFollowsTemplate = <N extends Node, E extends Edge>(path: Path<N, E>, template: string[]): boolean => {
110106
if (path.nodes.length !== template.length) {
111107
return false;
112108
}
@@ -119,4 +115,4 @@ export function pathFollowsTemplate<N extends Node, E extends Edge>(
119115
}
120116

121117
return true;
122-
}
118+
};

packages/algorithms/src/evaluation/path-planting/noise-generator.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* Noise path generation for evaluation
33
*/
44

5-
import type { Edge, Node } from '../../types/graph';
65
import { Graph } from '../../graph/graph';
76
import type { Path } from '../../types/algorithm-results';
7+
import type { Edge, Node } from '../../types/graph';
88

99
/**
1010
* Add noise paths to make ground truth detection harder.
@@ -20,12 +20,7 @@ import type { Path } from '../../types/algorithm-results';
2020
* @param seed - Random seed
2121
* @returns Graph with added noise paths
2222
*/
23-
export function addNoisePaths<N extends Node, E extends Edge>(
24-
graph: Graph<N, E>,
25-
groundTruth: Path<N, E>[],
26-
numNoisePaths: number,
27-
seed?: number
28-
): Graph<N, E> {
23+
export const addNoisePaths = <N extends Node, E extends Edge>(graph: Graph<N, E>, groundTruth: Path<N, E>[], numNoisePaths: number, seed?: number): Graph<N, E> => {
2924
if (numNoisePaths <= 0) {
3025
return graph;
3126
}
@@ -89,20 +84,19 @@ export function addNoisePaths<N extends Node, E extends Edge>(
8984
}
9085

9186
return graph;
92-
}
87+
};
9388

9489
/**
9590
* Create a simple path between two nodes.
91+
* @param graph
92+
* @param source
93+
* @param target
94+
* @param length
95+
* @param rng
96+
* @param nodeOffset
97+
* @param edgeOffset
9698
*/
97-
function createSimplePath<N extends Node, E extends Edge>(
98-
graph: Graph<N, E>,
99-
source: string,
100-
target: string,
101-
length: number,
102-
rng: SeededRandom,
103-
nodeOffset: number,
104-
edgeOffset: number
105-
): Path<N, E> {
99+
const createSimplePath = <N extends Node, E extends Edge>(graph: Graph<N, E>, source: string, target: string, length: number, rng: SeededRandom, nodeOffset: number, edgeOffset: number): Path<N, E> => {
106100
const nodes: N[] = [];
107101
const edges: E[] = [];
108102

@@ -159,14 +153,13 @@ function createSimplePath<N extends Node, E extends Edge>(
159153
edges,
160154
totalWeight: edges.reduce((sum, e) => sum + (e.weight ?? 0), 0),
161155
};
162-
}
156+
};
163157

164158
/**
165159
* Generate path signature for deduplication.
160+
* @param path
166161
*/
167-
function pathSignature<N extends Node, E extends Edge>(path: Path<N, E>): string {
168-
return path.edges.map(e => `${e.source}-${e.target}`).join('|');
169-
}
162+
const pathSignature = <N extends Node, E extends Edge>(path: Path<N, E>): string => path.edges.map(e => `${e.source}-${e.target}`).join('|');
170163

171164
/**
172165
* Seeded random number generator.
@@ -188,6 +181,8 @@ class SeededRandom {
188181

189182
/**
190183
* Generate random integer in [min, max].
184+
* @param min
185+
* @param max
191186
*/
192187
nextInt(min: number, max: number): number {
193188
return Math.floor(this.nextDouble() * (max - min + 1)) + min;

packages/algorithms/src/evaluation/path-planting/path-generator.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* Ground truth path planting for evaluation
33
*/
44

5-
import type { Edge, Node } from '../../types/graph';
65
import { Graph } from '../../graph/graph';
7-
import type { Path } from '../../types/algorithm-results';
86
import type { RankedPath } from '../../pathfinding/path-ranking';
7+
import type { Path } from '../../types/algorithm-results';
8+
import type { Edge, Node } from '../../types/graph';
99

1010
/**
1111
* Configuration for planted path generation.
@@ -82,13 +82,16 @@ class SeededRandom {
8282

8383
/**
8484
* Generate random integer in [min, max].
85+
* @param min
86+
* @param max
8587
*/
8688
nextInt(min: number, max: number): number {
8789
return Math.floor(this.next() * (max - min + 1)) + min;
8890
}
8991

9092
/**
9193
* Shuffle array in place.
94+
* @param array
9295
*/
9396
shuffle<T>(array: T[]): T[] {
9497
const result = [...array];
@@ -102,8 +105,9 @@ class SeededRandom {
102105

103106
/**
104107
* Convert signal strength to MI value range.
108+
* @param signalStrength
105109
*/
106-
function signalStrengthToMI(signalStrength: 'weak' | 'medium' | 'strong'): { min: number; max: number } {
110+
const signalStrengthToMI = (signalStrength: 'weak' | 'medium' | 'strong'): { min: number; max: number } => {
107111
switch (signalStrength) {
108112
case 'weak':
109113
return { min: 0.1, max: 0.3 }; // Low MI, hard to distinguish
@@ -112,7 +116,7 @@ function signalStrengthToMI(signalStrength: 'weak' | 'medium' | 'strong'): { min
112116
case 'strong':
113117
return { min: 0.8, max: 1.0 }; // High MI, easy to distinguish
114118
}
115-
}
119+
};
116120

117121
/**
118122
* Plant ground truth paths in a graph for evaluation.
@@ -128,10 +132,7 @@ function signalStrengthToMI(signalStrength: 'weak' | 'medium' | 'strong'): { min
128132
* @param config - Planting configuration
129133
* @returns Graph with planted paths and ground truth
130134
*/
131-
export function plantGroundTruthPaths<N extends Node, E extends Edge>(
132-
baseGraph: Graph<N, E>,
133-
config: PlantedPathConfig<N, E>
134-
): PlantedPathResult<N, E> {
135+
export const plantGroundTruthPaths = <N extends Node, E extends Edge>(baseGraph: Graph<N, E>, config: PlantedPathConfig<N, E>): PlantedPathResult<N, E> => {
135136
const rng = new SeededRandom(config.seed);
136137
const miRange = signalStrengthToMI(config.signalStrength);
137138

@@ -256,11 +257,10 @@ export function plantGroundTruthPaths<N extends Node, E extends Edge>(
256257
avgPathMI: totalMI / plantedPaths.length,
257258
},
258259
};
259-
}
260+
};
260261

261262
/**
262263
* Generate stable path ID for relevance scoring.
264+
* @param path
263265
*/
264-
function pathId<N extends Node, E extends Edge>(path: Path<N, E>): string {
265-
return path.nodes.map(n => n.id).join('→');
266-
}
266+
const pathId = <N extends Node, E extends Edge>(path: Path<N, E>): string => path.nodes.map(n => n.id).join('→');

0 commit comments

Comments
 (0)