JavaScript Snippet

Partition an Array by a Predicate

Difficulty: Medium

Calling `array.filter(p)` and `array.filter((x) => !p(x))` works but walks the input twice and runs the predicate twice per element, which is wasteful and (for non-pure predicates) plain wrong. A single-pass `partition` returns the matched and unmatched buckets in one go. This snippet covers a clean fold-based implementation, an N-way `partitionBy` for multi-class splits, and a streaming variant that lazily partitions an iterable without materialising the full input.

Code Snippets
/

Partition an Array by a Predicate

Partition an Array by a Predicate

Calling `array.filter(p)` and `array.filter((x) => !p(x))` works but walks the input twice and runs the predicate twice per element, which is wasteful and (for non-pure predicates) plain wrong. A single-pass `partition` returns the matched and unmatched buckets in one go. This snippet covers a clean fold-based implementation, an N-way `partitionBy` for multi-class splits, and a streaming variant that lazily partitions an iterable without materialising the full input.

JavaScript
Medium
4 snippets
arrays
utility
array-manipulation-patterns
functional-programming

758 views

18

A classic for loop runs the predicate exactly once per element and pushes into one of two buckets. Returning a tuple [pass, fail] lets the caller destructure cleanly: const [yes, no] = partition(...). Compared to [arr.filter(p), arr.filter(notP)], this version is twice as fast on large inputs and correct even when the predicate is impure (e.g. randomised, time-based, or one that mutates a counter). The third predicate argument matches the Array.prototype.filter contract (item, index, array), so any predicate that works with filter is a drop-in here.