Proposal? foldAlt :: (Foldable t, Alternative f) => t a -> f a

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Proposal? foldAlt :: (Foldable t, Alternative f) => t a -> f a

Fumiaki Kinoshita
We found this quite useful, but we are not 100% about the name and documentation.

-- | Try—in the 'Alternative' sense—to return all the values in a 'Foldable'
-- container.
--
-- @
-- foldAlt ≡ 'listToMaybe' :: [a] -> 'Maybe' a
-- foldAlt ≡ 'maybeToList' :: 'Maybe' a -> [a]
-- foldAlt ≡ 'MaybeT' . 'return' :: ('Monad' m) => 'Maybe' a -> 'MaybeT' m a
-- foldAlt ≡ 'Pipes.ListT' . 'Pipes.each' :: ('Monad' m) => [a] -> 'Pipes.ListT' m a
-- foldAlt ≡ id :: 'Maybe' a -> 'Maybe' a
-- @
foldAlt :: (Foldable t, Alternative f) => t a -> f a
foldAlt = getAlt . foldMap (Alt . pure)
{-# INLINE foldAlt #-}

I propose adding this to either `Data.Foldable` or `Control.Applicative`. Any thoughts?

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Proposal? foldAlt :: (Foldable t, Alternative f) => t a -> f a

Tony Morris-4

I remember discussing this function about 8 or so years ago, in that it generalises by composition.

Here it is being discussed in Scala in 2012, including the generalisation by using the composition of functors. Sorry I couldn't find the earlier Haskell discussions. They are out there somewhere.
https://groups.google.com/forum/#!msg/scala-user/O_udbztHFHU/mpqPtEkjbb8J

However, since the adoption of the lens library since, I have preferred generalising based on Cons/Empty
https://github.com/qfpl/papa/blob/master/papa-lens-implement/src/Papa/Lens/Implement/Data/Collapse.hs


On 29/11/17 14:12, Fumiaki Kinoshita wrote:
We found this quite useful, but we are not 100% about the name and documentation.

-- | Try—in the 'Alternative' sense—to return all the values in a 'Foldable'
-- container.
--
-- @
-- foldAlt ≡ 'listToMaybe' :: [a] -> 'Maybe' a
-- foldAlt ≡ 'maybeToList' :: 'Maybe' a -> [a]
-- foldAlt ≡ 'MaybeT' . 'return' :: ('Monad' m) => 'Maybe' a -> 'MaybeT' m a
-- foldAlt ≡ 'Pipes.ListT' . 'Pipes.each' :: ('Monad' m) => [a] -> 'Pipes.ListT' m a
-- foldAlt ≡ id :: 'Maybe' a -> 'Maybe' a
-- @
foldAlt :: (Foldable t, Alternative f) => t a -> f a
foldAlt = getAlt . foldMap (Alt . pure)
{-# INLINE foldAlt #-}

I propose adding this to either `Data.Foldable` or `Control.Applicative`. Any thoughts?


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Proposal? foldAlt :: (Foldable t, Alternative f) => t a -> f a

Andrew Martin
In reply to this post by Fumiaki Kinoshita
+1 from me. We probably also want foldMapAlt:

    foldMapAlt :: (Foldable t, Alternative f) => (a -> f b) -> t a -> f b

On Tue, Nov 28, 2017 at 11:12 PM, Fumiaki Kinoshita <[hidden email]> wrote:
We found this quite useful, but we are not 100% about the name and documentation.

-- | Try—in the 'Alternative' sense—to return all the values in a 'Foldable'
-- container.
--
-- @
-- foldAlt ≡ 'listToMaybe' :: [a] -> 'Maybe' a
-- foldAlt ≡ 'maybeToList' :: 'Maybe' a -> [a]
-- foldAlt ≡ 'MaybeT' . 'return' :: ('Monad' m) => 'Maybe' a -> 'MaybeT' m a
-- foldAlt ≡ 'Pipes.ListT' . 'Pipes.each' :: ('Monad' m) => [a] -> 'Pipes.ListT' m a
-- foldAlt ≡ id :: 'Maybe' a -> 'Maybe' a
-- @
foldAlt :: (Foldable t, Alternative f) => t a -> f a
foldAlt = getAlt . foldMap (Alt . pure)
{-# INLINE foldAlt #-}

I propose adding this to either `Data.Foldable` or `Control.Applicative`. Any thoughts?

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries




--
-Andrew Thaddeus Martin

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Proposal? foldAlt :: (Foldable t, Alternative f) => t a -> f a

David Feuer
I neither support nor oppose this in principle, but I definitely would
only support
it with a more clever implementation. The one above relies on arity analysis or
inlining to avoid building a lousy closure representing (Alt . pure).
foldMapAlt is really
nothing more than foldMap applied to a particular Monoid dictionary:

  foldMapAlt :: forall t f a b. (Foldable t, Alternative f) => (a -> f
b) -> t a -> f b
  foldMapAlt = coerce (foldMap :: (a -> Alt f b) -> t a -> Alt f b)

  foldAlt :: (Foldable t, Alternative f) => t a -> f a
  foldAlt = foldMapAlt pure

David Feuer

On Wed, Nov 29, 2017 at 7:01 AM, Andrew Martin
<[hidden email]> wrote:

> +1 from me. We probably also want foldMapAlt:
>
>     foldMapAlt :: (Foldable t, Alternative f) => (a -> f b) -> t a -> f b
>
> On Tue, Nov 28, 2017 at 11:12 PM, Fumiaki Kinoshita <[hidden email]>
> wrote:
>>
>> We found this quite useful, but we are not 100% about the name and
>> documentation.
>>
>> -- | Try—in the 'Alternative' sense—to return all the values in a
>> 'Foldable'
>> -- container.
>> --
>> -- @
>> -- foldAlt ≡ 'listToMaybe' :: [a] -> 'Maybe' a
>> -- foldAlt ≡ 'maybeToList' :: 'Maybe' a -> [a]
>> -- foldAlt ≡ 'MaybeT' . 'return' :: ('Monad' m) => 'Maybe' a -> 'MaybeT' m
>> a
>> -- foldAlt ≡ 'Pipes.ListT' . 'Pipes.each' :: ('Monad' m) => [a] ->
>> 'Pipes.ListT' m a
>> -- foldAlt ≡ id :: 'Maybe' a -> 'Maybe' a
>> -- @
>> foldAlt :: (Foldable t, Alternative f) => t a -> f a
>> foldAlt = getAlt . foldMap (Alt . pure)
>> {-# INLINE foldAlt #-}
>>
>> I propose adding this to either `Data.Foldable` or `Control.Applicative`.
>> Any thoughts?
>>
>> _______________________________________________
>> Libraries mailing list
>> [hidden email]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>>
>
>
>
> --
> -Andrew Thaddeus Martin
>
> _______________________________________________
> Libraries mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries