ekmett / bifunctors

Haskell 98 bifunctors, bifoldables and bitraversables
Other
57 stars 42 forks source link

Add function that map the same function on both parameters #63

Open jachymb opened 7 years ago

jachymb commented 7 years ago

I often use something like this:

bothmap :: Bifunctor f => (a -> b) -> f a a -> f b b
bothmap f = bimap f f

I typically use it with pairs where function f is a lambda expression and I'm lazy to bind it to a name. It would be nice to have it in the library.

RyanGlScott commented 7 years ago

Personally, I'm not sure if this crosses the Fairbairn threshold in my mind, since literally every bifunctorial function in this library could have a variant like this. I'm similarly skeptical about combinators like ~bimap f id~, bifoldMap f (const mempty), etc.

That being said, Data.Bifunctor.Join already gives you what you want:

bothmap :: Bifunctor f => (a -> b) -> f a a -> f b b
bothmap f = runJoin . fmap f . Join
jachymb commented 7 years ago

bimap f id = second, no?

RyanGlScott commented 7 years ago

Oops, I misspoke about bimap f id, then.

jachymb commented 7 years ago

btw, speaking of "join", you can of course also have:

import Control.Monad (join)
bothmap :: Bifunctor f => (a -> b) -> f a a -> f b b
bothmap = join bimap
phadej commented 5 years ago

We have both in lens. We could had it in Data.Bitraversable, but not sure it worth the trouble.

join bimap
bothmap

Former is not much longer, is it?