Skip to content

Data.Monoid Last is using default mconcat, implement with foldl will improve the performance

Motivation

As shown in the following code, the Last type inside Data.Monoid is using default mconcat implementation which uses foldr.

-- | @since 2.01
instance Monoid (Last a) where
        mempty = Last Nothing

The <> for Last is lazy in its first argument, so using foldl could be more efficient.

-- | @since 4.9.0.0
instance Semigroup (Last a) where
        a <> Last Nothing = a
        _ <> b                   = b
        stimes = stimesIdempotentMonoid

Proposal

Add custom mconcat implementation using foldl for type Last.

Edited by Junjie Wu
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information