Implement sequence
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
export function minByOrNull<T>(value: Iterable<T>, transform: (it: T) => number): T | null {
|
||||
let minIt!: T;
|
||||
let min!: number;
|
||||
let found = false;
|
||||
let minVal: number | undefined;
|
||||
for (const it of value) {
|
||||
const val = transform(it);
|
||||
if (found && val >= min) continue;
|
||||
minIt = it;
|
||||
min = val;
|
||||
found = true;
|
||||
if (minVal === undefined || val < minVal) {
|
||||
minIt = it;
|
||||
minVal = val;
|
||||
}
|
||||
}
|
||||
if (!found) return null;
|
||||
if (minVal === undefined) return null;
|
||||
return minIt;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user