I pushed a pull request to Edward Kmett’s either package to implement two functions some guys was complaining not to find:
flipEither :: Either e a -> Either a e and
flipEitherT :: EitherT e m a -> EitherT a m e.
When implementing the functions, I wondered: “Hey, flipping stuff is a pretty common operation. Don’t we have an abstraction for that yet?”. I haven’t found any.
I decided to make a little typeclass to see what it’d be.
class Swap s whereswap :: s a b -> s b ainstance Swap (,) whereswap (a,b) = (b,a)instance Swap Either whereswap = flipEither-- let’s go wild and fooledinstance Swap Map whereswap = fromList . fmap swap . toList
If you think that’s handy, I’ll write a little package with default instances to make it live.
Happy hacking folks!