Skip to content

Instantly share code, notes, and snippets.

@zachlysobey
Last active April 6, 2023 16:33
Show Gist options
  • Save zachlysobey/71ac85046d0d533287ed85e1caa64660 to your computer and use it in GitHub Desktop.
Save zachlysobey/71ac85046d0d533287ed85e1caa64660 to your computer and use it in GitHub Desktop.
TypeScript array `partition` utility
/**
* 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
}
@connorads
Copy link

noice

@drewish
Copy link

drewish commented Apr 6, 2023

Appreciate this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment