UNPKG

froebel

Version:
20 lines (19 loc) 644 B
/** * Takes a `list` and returns a pair of lists containing: the elements that * match the `predicate` and those that don't, respectively. * * Think of it as `filter`, but the elements that don't pass the filter aren't * discarded but returned in a separate list instead. * * @example * ``` * const [strings, numbers] = partition( * ['a', 'b', 1, 'c', 2, 3], * (el): el is string => typeof el === 'string' * ) * // strings: ["a", "b", "c"] * // numbers: [1, 2, 3] * ``` */ const partition = (list, predicate) => list.reduce(([t, f], c) => predicate(c) ? [[...t, c], f] : [t, [...f, c]], [[], []]); export default partition;