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

feat: Add partitionWith, partitionWithA, partitionEithers #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

414owen
Copy link

@414owen 414owen commented Jul 29, 2023

Provides a generic partitionWith, witherable seems to be the logical place for such a thing, as it can be implemented in terms of the existing Filterable class.

I added it to the Filterable class because more efficient versions may be available.

Existing names I've found for this operation: Vector's partitionWith, base's partitionEithers, containers' mapEither.

I chose partitionWith, and partitionEithers, but I don't have a strong opinion on the name (although I'd prefer not to add a new name to the haskell ecosystem).

I saw #45 after I'd written this, I'm afraid. Does anyone have any concerns about this implementation?

@phadej
Copy link
Collaborator

phadej commented Jul 29, 2023

I think I'd rather make relax laws of unalign :: f (These a b) -> (f a, f b) from semialign to allow sequence like instances, and provide partition there. It was asked there not so long time ago and I think it makes sense.

EDIT: I prefer that as unalign already exists.

@414owen
Copy link
Author

414owen commented Jul 31, 2023

Thanks @phadej,

The lack of [] and Vector instances of Unalign is a little unfortunate, as those are the concrete types that I want to use unalignWith/partitionWith for. I see that there's an issue for it here, but it requires dropping a law.

I'll add a law-breaking orphan instance to our codebase for now. Do you think the instances will end up making it into semialign?

If not, having the methods on Filterable might still be a good option?

@phadej
Copy link
Collaborator

phadej commented Jul 31, 2023

Do you think the instances will end up making it into semialign?

Yes.

@echatav
Copy link

echatav commented Sep 22, 2024

partitionEithers should definitely be included in witherable! It identifies Filterable as an oplax monoidal functor between coproduct and product.

const () :: f Void -> ()
partitionEithers :: Filterable f => f (Either a b) -> (f a, f b)

When f is also Alternative, we can say there is a strong monoidal functor between coproduct and product if the below are inverse to the above:

const empty :: Alternative f => () -> f Void
plusA :: Alternative f => (f a, f b) -> f (Either a b)
plusA (a,b) = Left <$> a <|> Right <$> b

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

Successfully merging this pull request may close these issues.

3 participants