mirror of
https://github.com/github/semantic.git
synced 2024-12-21 13:51:44 +03:00
Speed up foldMapA.
Some research in https://github.com/fused-effects/diffused-effects/pull/1 revealed that GHC sometimes fails to optimize newtype-unwrappers of the foldMapA style sufficiently, and that you get better Core if you rub some `coerce` on it. Since we use `foldMapA` in a lot of places, this should have a positive impact.
This commit is contained in:
parent
af35b2225a
commit
8bf42329a4
@ -1,4 +1,5 @@
|
||||
{-# LANGUAGE UndecidableInstances #-}
|
||||
{-# OPTIONS_GHC -fplugin=DumpCore #-}
|
||||
module Prologue
|
||||
( module X
|
||||
, eitherM
|
||||
@ -66,8 +67,8 @@ import GHC.Stack as X
|
||||
|
||||
-- | Fold a collection by mapping each element onto an 'Alternative' action.
|
||||
foldMapA :: (Alternative m, Foldable t) => (b -> m a) -> t b -> m a
|
||||
foldMapA f = getAlt . foldMap (Alt . f)
|
||||
|
||||
foldMapA f = getAlt #. foldMap (Alt #. f)
|
||||
{-# INLINE foldMapA #-}
|
||||
|
||||
maybeLast :: Foldable t => b -> (a -> b) -> t a -> b
|
||||
maybeLast b f = maybe b f . getLast . foldMap (Last . Just)
|
||||
@ -84,3 +85,7 @@ maybeM f = maybe f pure
|
||||
eitherM :: Applicative f => (a -> f b) -> Either a b -> f b
|
||||
eitherM f = either f pure
|
||||
{-# INLINE eitherM #-}
|
||||
|
||||
(#.) :: Coercible b c => (b -> c) -> (a -> b) -> (a -> c)
|
||||
(#.) _ = coerce
|
||||
{-# INLINE (#.) #-}
|
||||
|
Loading…
Reference in New Issue
Block a user