[Haskell-cafe] mapFst and mapSnd

Andreas Abel andreas.abel at ifi.lmu.de
Tue May 28 16:01:07 CEST 2013


See Agda.Utils.Tuple :-)

-- | Bifunctoriality for pairs.
(-*-) :: (a -> c) -> (b -> d) -> (a,b) -> (c,d)
(f -*- g) ~(x,y) = (f x, g y)

-- | @mapFst f = f -*- id@
mapFst :: (a -> c) -> (a,b) -> (c,b)
mapFst f ~(x,y) = (f x, y)

-- | @mapSnd g = id -*- g@
mapSnd :: (b -> d) -> (a,b) -> (a,d)
mapSnd g ~(x,y) = (x, g y)

I think mapPair, mapFst, and mapSnd are canonical names that could be 
added to Data.Tuple.  But if you suggest this on libraries at haskell.org, 
you get probably turned down, see e.g.

   http://comments.gmane.org/gmane.comp.lang.haskell.libraries/17411

Cheers,
Andreas

On 28.05.2013 15:34, Petr Pudlák wrote:
> Dne 28.5.2013 10:54, Dominique Devriese napsal(a):
>> Hi all,
>>
>> I often find myself needing the following definitions:
>>
>>    mapPair :: (a -> b) -> (c -> d) -> (a,c) -> (b,d)
>>    mapPair f g (x,y) = (f x, g y)
>>
>>    mapFst :: (a -> b) -> (a,c) -> (b,c)
>>    mapFst f = mapPair f id
>>
>>    mapSnd :: (b -> c) -> (a,b) -> (a,c)
>>    mapSnd = mapPair id
>>
>> But they seem missing from the prelude and Hoogle or Hayoo only turn
>> up versions of them in packages like scion or fgl.  Has anyone else
>> felt the need for these functions?  Am I missing some generalisation
>> of them perhaps?
> Apart from Arrows, there is also package "bifunctors" that defines this
> functionality for (,), Either and a few others:
> http://hackage.haskell.org/packages/archive/bifunctors/3.2.0.1/doc/html/Data-Bifunctor.html
>
>
> Petr Pudlak
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


-- 
Andreas Abel  <><      Du bist der geliebte Mensch.

Theoretical Computer Science, University of Munich
Oettingenstr. 67, D-80538 Munich, GERMANY

andreas.abel at ifi.lmu.de
http://www2.tcs.ifi.lmu.de/~abel/



More information about the Haskell-Cafe mailing list