Data.Tuple impoverished

Stephen Tetley stephen.tetley at gmail.com
Fri Jun 15 19:03:16 CEST 2012


> mapPair :: (a -> c, b -> d) -> (a, b) -> (c, d)
> mapFst :: (a -> c) -> (a, b) -> (c, b)
> mapSnd :: (b -> c) -> (a, b) -> (a, c)

These three could be replaced by a more general (and often defined)
bifunctor class

class Bifunctor f where
  bimap  :: (a -> c) -> (b -> d) -> f a b -> f c d
  mapFst :: (a -> c) -> f a b -> f c b	-- or mapL
  mapSnd :: (b -> d) -> f a b -> f a d  -- or mapR

This class also has a useful instance for Either in Base.

> fst3 :: (a, b, c) -> a
> snd3 :: (a, b, c) -> b
> thd3 :: (a, b, c) -> c


This naming scheme for extractors from pairs (then triples) doesn't
really extend to 4 tuples or higher, maybe a rethink is useful before
a commit to the Base libraries. Of the top of my head, fst and snd are
the only bits of "vowel dropping" as a naming scheme I can think of in
Base, maybe it is not a tradition to continue, though SML has a few
more like hd and tl.



More information about the Libraries mailing list