John Wiegley | 20 Nov 20:14 2012

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 | 20 Nov 20:16 2012

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

>>>>> John Wiegley <johnw <at> fpcomplete.com> 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
Andreas Abel | 20 Nov 20:19 2012
Picon

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

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
(Continue reading)

Edward Kmett | 20 Nov 20:23 2012
Picon

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

On Tue, Nov 20, 2012 at 2:14 PM, John Wiegley <johnw <at> fpcomplete.com> 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


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
 
_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
John Wiegley | 20 Nov 20:28 2012

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

>>>>> Edward Kmett <ekmett <at> gmail.com> 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

_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Dan Burton | 20 Nov 21:09 2012
Picon

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

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.
_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Darius Jahandarie | 20 Nov 21:17 2012
Picon

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

On Tue, Nov 20, 2012 at 3:09 PM, Dan Burton <danburton.email <at> gmail.com> wrote:
> 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
Gershom Bazerman | 20 Nov 21:33 2012
Picon

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

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
_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
wren ng thornton | 23 Nov 11:40 2012

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

On 11/20/12 2:23 PM, Edward Kmett wrote:
> On Tue, Nov 20, 2012 at 2:14 PM, John Wiegley <johnw <at> fpcomplete.com> 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
Bryan O'Sullivan | 20 Nov 20:24 2012

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

On Tue, Nov 20, 2012 at 11:14 AM, John Wiegley <johnw <at> fpcomplete.com> wrote:

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

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

I don't think these offer any value beyond saving a couple of keystrokes. -1 from me.
_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Twan van Laarhoven | 23 Nov 12:14 2012
Picon

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

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
Bertram Felgenhauer | 23 Nov 16:43 2012

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

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

Gmane