bifunctors icon indicating copy to clipboard operation
bifunctors copied to clipboard

Add function that map the same function on both parameters

Open jachymb opened this issue 7 years ago • 5 comments

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.

jachymb avatar Aug 16 '17 14:08 jachymb

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

RyanGlScott avatar Aug 16 '17 14:08 RyanGlScott

bimap f id = second, no?

jachymb avatar Aug 17 '17 09:08 jachymb

Oops, I misspoke about bimap f id, then.

RyanGlScott avatar Aug 17 '17 10:08 RyanGlScott

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

jachymb avatar Aug 17 '17 11:08 jachymb

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?

phadej avatar Jan 29 '19 11:01 phadej