Last active
April 6, 2023 16:33
-
-
Save zachlysobey/71ac85046d0d533287ed85e1caa64660 to your computer and use it in GitHub Desktop.
TypeScript array `partition` utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Takes a predicate and a list of values and returns a a tuple (2-item array), | |
* with each item containing the subset of the list that matches the predicate | |
* and the complement of the predicate respectively | |
* | |
* @sig (T -> Boolean, T[]) -> [T[], T[]] | |
* | |
* @param {Function} predicate A predicate to determine which side the element belongs to. | |
* @param {Array} arr The list to partition | |
* | |
* Inspired by the Ramda function of the same name | |
* @see https://ramdajs.com/docs/#partition | |
* | |
* @example | |
* | |
* const isNegative: (n: number) => boolean = n => n < 0 | |
* const numbers = [1, 2, -4, -7, 4, 22] | |
* partition(isNegative, numbers) | |
* // => [ [-4, -7], [1, 2, 4, 22] ] | |
*/ | |
function partition<T> ( | |
predicate: (val: T) => boolean, | |
arr: Array<T>, | |
): [Array<T>, Array<T>] { | |
const partitioned: [Array<T>, Array<T>] = [[], []] | |
arr.forEach((val: T) => { | |
const partitionIndex: 0 | 1 = predicate(val) ? 0 : 1 | |
partitioned[partitionIndex].push(val) | |
}) | |
return partitioned | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment