forked from apple/ml-hierarchical-confusion-matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetops.ts
23 lines (21 loc) · 733 Bytes
/
setops.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
* For licensing see accompanying LICENSE file.
*
* Copyright (C) 2022 Apple Inc. All Rights Reserved.
*/
// TODO: We could use generator functions to explore the full space with constand memory.
export function powerset<T>(elements: Array<T>): Array<Set<T>> {
return elements.reduce((acc: Array<Set<T>>, value: T) => {
return acc.concat(acc.map(set => new Set([value, ...set])));
}, [new Set<T>()]);
}
export function nest<T>(parent: Array<T>, sub: Array<T>): Array<Array<T>> {
// Prepopulate the array because sub is optional
const result = [...parent.map(p => [p])];
for (const p of parent) {
for (const s of sub) {
result.push([p, s]);
}
}
return result;
}