-
Notifications
You must be signed in to change notification settings - Fork 448
Add bifurcate(_:)
#151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bifurcate(_:)
#151
Conversation
Like `filter(_:)`, but also returns a collection of the elements for which the predicate returned `false`
Thanks for suggesting this @mdznr! This operation is commonly called Your implementation of the
There are some trade-offs here regarding performance, implementation simplicity, and having a clean API. A good next step would be to get our hands on some benchmark results that might tell us which directions are worth pursuing. |
I like that idea a lot. I don’t know how I missed the existing stable partition function in this package. 🤦♂️
I like that idea. That allows the caller to decide whether the conversation to
I see
Sounds like a good idea. I'll use https://github.com/apple/swift-collections-benchmark to guide this. |
GitHub won’t let me re-open this PR because I’ve force-pushed changes onto the branch. Following up with #152 |
Description
Adds a
bifurcate(_:)
algorithm. This is very similar tofilter(_:)
, but instead of just getting an array of the elements that did match the given predicate, also get a second array for the elements that did not match the given predicate.This is more performant than calling
filter(_:)
twice on the same input with mutually-exclusive predicates since:Detailed Design
Naming
It was hard to find a word to describe this behavior without using terms like “split” or “separate”, which might have other interpretations (like only being able to get the prefix and suffix), so I chose “bifurcate”, but definitely open to hearing different names.
For consistency with
filter(_:)
, I avoided calling this function something likebifurcating(_:)
,bifurcated(_:)
, or giving the parameter a name at the call-site, likebifurcate(where:)
.Documentation Plan
Test Plan
Source Impact
This is purely additive
Checklist