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

Splitter is a contravariant functor #21

Closed
eayus opened this issue Dec 21, 2023 · 4 comments
Closed

Splitter is a contravariant functor #21

eayus opened this issue Dec 21, 2023 · 4 comments

Comments

@eayus
Copy link
Contributor

eayus commented Dec 21, 2023

The Splitter type seems to form a contravariant functor: given a splitter than works on a and a preprocessing step b -> a, we can get a splitter that works on b.

The package cotravariant adds a type class for this, though if you wanted to avoid dependencies a simple function would also work.

Is this something you would consider adding to this library? I'd be happy to implement it.

@byorgey
Copy link
Owner

byorgey commented Dec 26, 2023

Hi @eayus , thanks for this suggestion! I definitely do not want to add a dependency on the contravariant package, since the split package currently only has a dependency on base, and adding a bunch of dependencies could have a big impact (currently 7441 packages on Hackage import split either directly or transitively).

However, I think you are right that Splitter is a contravariant functor and I would be happy to merge a PR that adds a function to witness this. Perhaps mapSplitter :: (b -> a) -> Splitter a -> Splitter b? I'm open to other naming suggestions as well. Also, I suppose while we're at it we should also add mapDelimiter :: (b -> a) -> Delimiter a -> Delimiter b, and mapSplitter would be implemented in terms of it.

@eayus
Copy link
Contributor Author

eayus commented Dec 28, 2023

I agree and think that those are sensible names for the functions. I'll try to get around to submitting a PR in the next few days.

@Bodigrim
Copy link

The package cotravariant adds a type class for this, though if you wanted to avoid dependencies a simple function would also work.

Contravariant has been in base for quite a long time: https://hackage.haskell.org/package/base-4.19.0.0/docs/Data-Functor-Contravariant.html#t:Contravariant

@byorgey
Copy link
Owner

byorgey commented Jan 11, 2024

Oh, thanks @Bodigrim , I had forgotten that! @eayus would you like to also add a Contravariant instance (just by setting contramap = mapSplitter)?

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

3 participants