Skip to content
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

split_after_first()'s return value can be awkward to use in some cases #2

Open
abingham opened this issue Dec 21, 2022 · 1 comment

Comments

@abingham
Copy link
Contributor

The fact that split_after_first returns either one or two lists makes it awkward to interpret its results in some cases. Consider:

>>> a, *b = split_after_first([1,2,3,4,5], lambda x: x == 5)
>>> a, b
([1, 2, 3, 4, 5], [])
>>> a, *b = split_after_first([1,2,3,4,5], lambda x: x == 6)
>>> a, b
([1, 2, 3, 4, 5], [])

Both of these calls produce identical results. In the first one, though, the predicate actually found a match, while in the second one it didn't. So if I'm trying to distinguish between a) only the last input value matching the predicate and b) no input values matching the predicate, I have to further examine the contents of the returned list. I.e. I can't easily know if it actually did "split after" or just didn't split at all.

If split_after_first() instead returned two lists all the time - one containing everything up to the first match (if there is one!), and the second containing everything else - then I'd always know how splitting occurred. That is:

>>> a, b = split_after_first([1,2,3,4,5], lambda x: x == 5)
>>> a, b
([1, 2, 3, 4, 5], [])
>>> a, b = split_after_first([1,2,3,4,5], lambda x: x == 6)
>>> a, b
([], [1, 2, 3, 4, 5])

I guess changing the API entirely is probably not a good idea. But could we add an argument to split_after_first which enables behavior like this? E.g.:

>>> a, b = split_after_first([1,2,3,4,5], lambda x: x == 6, explicit=True)
>>> a, b
([], [1, 2, 3, 4, 5])
@rob-smallshire
Copy link
Contributor

I dislike this function a lot. Maybe it should be called splat_after_first given it was originally developed to work with tuple unpacking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants