Adding swap/compose functions to Data.Tuple and Data.Function

John Wiegley johnw at fpcomplete.com
Tue Nov 20 20:14:57 CET 2012


Moving discussion from http://hackage.haskell.org/trac/ghc/ticket/7435:

I propose adding the following three functions to Data.Tuple and
Data.Function, respectively:

    swap :: (a,b) -> (b,a)
    swap = snd &&& fst

Fairly obvious, but strangely missing.

    compose :: [(a -> a)] -> a -> a
    compose = foldr (.) id

    composeM :: [(a -> m a)] -> a -> m a
    composeM = foldr (<=<) return

This can be done with Endo, of course, but the result is awkward. The idiom of
composing a list of endomorphisms occurs often enough that you can Google for
it.

-- 
John Wiegley
FP Complete                         Haskell tools, training and consulting
http://fpcomplete.com               johnw on #haskell/irc.freenode.net



More information about the Libraries mailing list