bifunctors
bifunctors copied to clipboard
Add function that map the same function on both parameters
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.
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
bimap f id = second
, no?
Oops, I misspoke about bimap f id
, then.
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
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?