import { assert, is } from "tsafe/assert"; import { ReduceArguments, toReduceArguments } from "./reduceify"; function arrPartitionImpl( arr: readonly ArrOf[], matcher: (entry: ArrOf) => entry is U ): [U[], Exclude[]] { return arr.reduce<[U[], Exclude[]]>((previousValue, currentValue) => { if (matcher(currentValue)) { previousValue[0].push(currentValue); } else { //NOTE: Should be deduced by the compiler assert(is>(currentValue)); previousValue[1].push(currentValue); } return previousValue; }, [[], []]); } export function arrPartition(arr: readonly ArrOf[], matcher: (entry: ArrOf) => entry is U): [U[], Exclude[]]; export function arrPartition(arr: readonly ArrOf[], matcher: (entry: ArrOf) => boolean): [ArrOf[], ArrOf[]]; export function arrPartition(arr: readonly ArrOf[], matcher: (entry: ArrOf) => boolean): [ArrOf[], ArrOf[]] { return arrPartitionImpl(arr, (entry: ArrOf): entry is ArrOf => matcher(entry)); } export function partition( matcher: (entry: ArrOf) => entry is U ): ReduceArguments[]]>; export function partition( matcher: (entry: ArrOf) => boolean ): ReduceArguments; export function partition( matcher: (entry: ArrOf) => boolean ): ReduceArguments | ReduceArguments[]]> { return toReduceArguments boolean]>(arrPartition, matcher); }