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

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

John Wiegley
writes:
swap :: (a,b) -> (b,a) swap = snd &&& fst
Fairly obvious, but strangely missing.
I'm sorry, I missed that this is already in Data.Tuple, as Hoogle does not find it. -- John Wiegley FP Complete Haskell tools, training and consulting http://fpcomplete.com johnw on #haskell/irc.freenode.net

On 20.11.2012 20:14, John Wiegley wrote:
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
+1
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.
+0.5 (a bit in doubt whether these are common enough). -- Andreas Abel <>< Du bist der geliebte Mensch. Theoretical Computer Science, University of Munich Oettingenstr. 67, D-80538 Munich, GERMANY andreas.abel@ifi.lmu.de http://www2.tcs.ifi.lmu.de/~abel/

On Tue, Nov 20, 2012 at 2:14 PM, John Wiegley
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
Data.Tuple already contains swap. Fairly obvious, but strangely missing.
compose :: [(a -> a)] -> a -> a compose = foldr (.) id
composeM :: [(a -> m a)] -> a -> m a composeM = foldr (<=<) return
I would actually consider these to fall beneath the "Fairbairn threshold". The composition of them is obvious, and the suggested names already exist commonly in a lot of third party code for all sorts of purposes. The pain of adoption plus the annoyance of trying to find them exceeds, to me, the utility of adding them. -Edward

Edward Kmett
writes:
Data.Tuple already contains swap.
Yeah, and I'm finding it just fine with Hoogle now. I'm not sure how I was led into thinking it didn't exist, but I had reached for it one day and my hands came up empty.
compose :: [(a -> a)] -> a -> a compose = foldr (.) id
composeM :: [(a -> m a)] -> a -> m a composeM = foldr (<=<) return
The composition of them is obvious, and the suggested names already exist commonly in a lot of third party code for all sorts of purposes.
The pain of adoption plus the annoyance of trying to find them exceeds, to me, the utility of adding them.
Fair enough. It does seem that the idiom deserves a name, though, considering how often I've come across it, but perhaps that ship has sailed. -- John Wiegley FP Complete Haskell tools, training and consulting http://fpcomplete.com johnw on #haskell/irc.freenode.net

It does seem that the idiom deserves a name, though, considering how often I've come across it.
The name for this is clearly mconcat, we just have to indicate *which* mconcat it is: the mconcat a la Endofunctors and Kleisli... whatevers, respectively. It seems like this sort of thing should be handled by the "newtype" package, but I can't seem to wrangle it into submission.

On Tue, Nov 20, 2012 at 3:09 PM, Dan Burton
The name for this is clearly mconcat, we just have to indicate which mconcat it is: the mconcat a la Endofunctors and Kleisli... whatevers, respectively. It seems like this sort of thing should be handled by the "newtype" package, but I can't seem to wrangle it into submission.
I'd be happy to take any needed steps to prepare or help push that package to some place more visible/useful. -- Darius Jahandarie

On 11/20/12 3:09 PM, Dan Burton wrote:
It does seem that the idiom deserves a name, though, considering how often I've come across it.
The name for this is clearly mconcat, we just have to indicate /which/ mconcat it is: the mconcat a la Endofunctors and Kleisli... whatevers, respectively. It seems like this sort of thing should be handled by the "newtype" package, but I can't seem to wrangle it into submission.
ala Endo foldMap [(+1),(+1),(+2)] $ 3
7 -g

On 11/20/12 2:23 PM, Edward Kmett wrote:
On Tue, Nov 20, 2012 at 2:14 PM, John Wiegley
wrote: compose :: [(a -> a)] -> a -> a compose = foldr (.) id
composeM :: [(a -> m a)] -> a -> m a composeM = foldr (<=<) return
I would actually consider these to fall beneath the "Fairbairn threshold".
The composition of them is obvious, and the suggested names already exist commonly in a lot of third party code for all sorts of purposes.
The pain of adoption plus the annoyance of trying to find them exceeds, to me, the utility of adding them.
Ditto. There's nothing especially interesting/unintuitive about the implementation, and while it occurs it's far from common enough to be worth the cost. So -1 from me. -- Live well, ~wren

On 20/11/12 20:14, John Wiegley wrote:
Fairly obvious, but strangely missing.
compose :: [(a -> a)] -> a -> a compose = foldr (.) id
I have never used this function in a generic setting, but I do often use concatShows :: [ShowS] -> ShowS concatShows = foldr (.) id Twan

John Wiegley wrote:
Fairly obvious, but strangely missing.
compose :: [(a -> a)] -> a -> a compose = foldr (.) id
-0.5
composeM :: [(a -> m a)] -> a -> m a composeM = foldr (<=<) return
-1 For some reason I would actually expect foldr (>=>) return. Either variant is bound to cause confusion for some people. Bertram
participants (10)
-
Andreas Abel
-
Bertram Felgenhauer
-
Bryan O'Sullivan
-
Dan Burton
-
Darius Jahandarie
-
Edward Kmett
-
Gershom Bazerman
-
John Wiegley
-
Twan van Laarhoven
-
wren ng thornton