chore: add a function to compare two arrays
This commit is contained in:
parent
d1d0328f8b
commit
5e2b8d3be0
1 changed files with 15 additions and 1 deletions
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue