/**
* Partition the values of a `HashSet` using the specified predicate.
*
* If a value matches the predicate, it will be placed into the `HashSet` on the
* right side of the resulting `Tuple`, otherwise the value will be placed into
* the left side.
*
* @tsplus static HashSet.Aspects partition
* @tsplus pipeable HashSet partition
*/
export function partition(
f: Refinement
): (self: HashSet) => readonly [HashSet, HashSet]
export function partition(
f: Predicate
): (self: HashSet) => readonly [HashSet, HashSet]
export function partition(
f: Predicate
) {
return (self: HashSet): readonly [HashSet, HashSet] => {
const vs = self.values
let e: IteratorResult
const right = HashSet.empty().beginMutation
const left = HashSet.empty().beginMutation
while (!(e = vs.next()).done) {
const value = e.value
if (f(value)) {
right.add(value)
} else {
left.add(value)
}
}
return [left.endMutation, right.endMutation]
}
}