mizzkey/src/prelude/array.ts

139 lines
3.3 KiB
TypeScript
Raw Normal View History

2018-12-19 09:08:09 +01:00
import { EndoRelation, Predicate } from './relation';
2018-12-19 08:59:43 +01:00
2018-12-19 01:54:45 +01:00
/**
* Count the number of elements that satisfy the predicate
*/
2018-12-19 09:08:09 +01:00
export function countIf<T>(f: Predicate<T>, xs: T[]): number {
2018-09-05 19:16:08 +02:00
return xs.filter(f).length;
}
2018-12-19 01:54:45 +01:00
/**
* Count the number of elements that is equal to the element
*/
export function count<T>(a: T, xs: T[]): number {
return countIf(x => x === a, xs);
2018-09-05 19:16:08 +02:00
}
2018-09-05 19:28:04 +02:00
2018-12-19 01:54:45 +01:00
/**
* Concatenate an array of arrays
*/
2018-09-06 14:31:15 +02:00
export function concat<T>(xss: T[][]): T[] {
return ([] as T[]).concat(...xss);
}
2018-12-19 01:54:45 +01:00
/**
* Intersperse the element between the elements of the array
* @param sep The element to be interspersed
*/
2018-09-05 19:28:04 +02:00
export function intersperse<T>(sep: T, xs: T[]): T[] {
2018-09-06 14:31:15 +02:00
return concat(xs.map(x => [sep, x])).slice(1);
2018-09-05 19:28:04 +02:00
}
2018-09-06 17:02:55 +02:00
2018-12-19 01:54:45 +01:00
/**
* Returns the array of elements that is not equal to the element
*/
export function erase<T>(a: T, xs: T[]): T[] {
return xs.filter(x => x !== a);
2018-09-06 17:02:55 +02:00
}
2018-09-06 17:10:03 +02:00
/**
* Finds the array of all elements in the first array not contained in the second array.
* The order of result values are determined by the first array.
*/
2018-12-19 01:14:05 +01:00
export function difference<T>(xs: T[], ys: T[]): T[] {
return xs.filter(x => !ys.includes(x));
2018-11-09 03:01:55 +01:00
}
2018-12-19 01:54:45 +01:00
/**
* Remove all but the first element from every group of equivalent elements
*/
2018-09-06 17:10:03 +02:00
export function unique<T>(xs: T[]): T[] {
return [...new Set(xs)];
}
2018-09-06 21:21:04 +02:00
export function sum(xs: number[]): number {
return xs.reduce((a, b) => a + b, 0);
}
2018-11-09 05:03:46 +01:00
export function maximum(xs: number[]): number {
return Math.max(...xs);
}
2018-12-19 01:54:45 +01:00
/**
* Splits an array based on the equivalence relation.
2018-12-19 02:02:58 +01:00
* The concatenation of the result is equal to the argument.
2018-12-19 01:54:45 +01:00
*/
2018-12-19 08:59:43 +01:00
export function groupBy<T>(f: EndoRelation<T>, xs: T[]): T[][] {
2018-11-09 05:03:46 +01:00
const groups = [] as T[][];
for (const x of xs) {
if (groups.length !== 0 && f(groups[groups.length - 1][0], x)) {
groups[groups.length - 1].push(x);
} else {
groups.push([x]);
}
}
return groups;
}
2018-12-19 01:54:45 +01:00
/**
* Splits an array based on the equivalence relation induced by the function.
2018-12-19 02:02:58 +01:00
* The concatenation of the result is equal to the argument.
2018-12-19 01:54:45 +01:00
*/
2018-11-09 05:03:46 +01:00
export function groupOn<T, S>(f: (x: T) => S, xs: T[]): T[][] {
return groupBy((a, b) => f(a) === f(b), xs);
}
export function groupByX<T>(collections: T[], keySelector: (x: T) => string) {
return collections.reduce((obj: Record<string, T[]>, item: T) => {
const key = keySelector(item);
if (!obj.hasOwnProperty(key)) {
obj[key] = [];
}
obj[key].push(item);
return obj;
}, {});
}
2018-12-19 01:54:45 +01:00
/**
* Compare two arrays by lexicographical order
*/
export function lessThan(xs: number[], ys: number[]): boolean {
for (let i = 0; i < Math.min(xs.length, ys.length); i++) {
if (xs[i] < ys[i]) return true;
if (xs[i] > ys[i]) return false;
}
return xs.length < ys.length;
}
2018-12-02 12:28:22 +01:00
2018-12-19 01:54:45 +01:00
/**
* Returns the longest prefix of elements that satisfy the predicate
*/
2018-12-19 09:08:09 +01:00
export function takeWhile<T>(f: Predicate<T>, xs: T[]): T[] {
2018-12-02 12:28:22 +01:00
const ys = [];
for (const x of xs) {
if (f(x)) {
ys.push(x);
} else {
break;
}
}
return ys;
}
export function cumulativeSum(xs: number[]): number[] {
const ys = Array.from(xs); // deep copy
for (let i = 1; i < ys.length; i++) ys[i] += ys[i - 1];
return ys;
}
export function toArray<T>(x: T | T[] | undefined): T[] {
return Array.isArray(x) ? x : x != null ? [x] : [];
}
export function toSingle<T>(x: T | T[] | undefined): T | undefined {
return Array.isArray(x) ? x[0] : x;
}