DRAFT: implement sequence
This commit is contained in:
14
src/sequence/maxByOrNull.ts
Normal file
14
src/sequence/maxByOrNull.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
export function maxByOrNull<T>(value: Iterable<T>, transform: (it: T) => number): T | null {
|
||||
let maxIt!: T;
|
||||
let max!: number;
|
||||
let found = false;
|
||||
for (const it of value) {
|
||||
const val = transform(it);
|
||||
if (found && val <= max) continue;
|
||||
maxIt = it;
|
||||
max = val;
|
||||
found = true;
|
||||
}
|
||||
if (!found) return null;
|
||||
return maxIt;
|
||||
}
|
||||
Reference in New Issue
Block a user