Strict folds without base case to `Data.Foldable`.

Viktor Dukhovni ietf-dane at dukhovni.org
Wed Jun 30 19:52:06 UTC 2021


On Thu, Jul 01, 2021 at 12:01:51AM +0500, Ignat Insarov wrote:

> Whichever floats your boats folks. I am a user of Haskell and I
> require strict folds without base case. Further I dare not meddle.

To what sort of structure(s) are looking to apply a strict foldl1?

Since there is no `foldl1'` class method to override, no structures
have a specialised definition.  Therefore, a "default" definition
can be written outside the class:

    foldl1' :: Foldable t => (a -> a -> a) -> t a -> a
    foldl1' f = fromMaybe (errorWithoutStackTrace "foldl1': empty structure")
        . foldl' f' Nothing
      where
        f' ma x = Just $! case ma of
             Nothing -> x
             Just a  -> f a x

Which means you can just use this in your own code, it need not first
land in the Data.Foldable module for you to make use of it.

-- 
    Viktor.

P.S.  FWIW, apologies if I am misreading the tone of your message, but
it does sound "demanding" to me.  GHC is a free community project with
no large corporation invested in its development, all the contributors
want to help users, but this is easier to help users who are considerate
of those to would volunteer their time to help.


More information about the Libraries mailing list