chore: add a function to compare two arrays

This commit is contained in:
naskya 2024-03-26 19:43:11 +09:00
parent d1d0328f8b
commit 5e2b8d3be0
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C

View file

@ -3,7 +3,6 @@ import type { EndoRelation, Predicate } from "./relation.js";
/**
* Count the number of elements that satisfy the predicate
*/
export function countIf<T>(f: Predicate<T>, xs: T[]): number {
return xs.filter(f).length;
}
@ -60,6 +59,21 @@ export function maximum(xs: number[]): number {
return Math.max(...xs);
}
/**
* Returns if two arrays are equal
*
* examples:
* equal([1, 2, 3], [1, 2, 3]) === true
* equal([1, 2, 3], [1, 3, 2]) === false
* equal([1, 2, 3], [1, 2]) === false
*/
export function equal<T>(lhs: T[], rhs: T[]): boolean {
return (
lhs.length === rhs.length &&
lhs.every((value, index) => value === rhs[index])
);
}
/**
* Splits an array based on the equivalence relation.
* The concatenation of the result is equal to the argument.