Skip to content

Reexport Ap and change definition of foldMapA to use Ap #121

@chessai

Description

@chessai

current definition of foldMapA:

foldMapA :: forall b m f a . (Monoid b, Applicative m, Foldable f) => (a -> m b) -> f a -> m b
foldMapA f = foldr (\a mb -> liftA2 mappend (f a) mb) (pure mempty)

instead of using an explicit right fold, it might be better to use whatever foldMap is, using Data.Monoid.Ap:

newtype Ap f a = Ap { getAp :: f a }
  deriving (Functor, Applicative)

instance (Applicative f, Semigroup a) => Semigroup (Ap f a) where
  x <> y = liftA2 (<>) x y

instance (Applicative f, Monoid a) => Monoid (Ap f a) where
  mempty = pure mempty

foldMapA :: (Monoid b, Foldable t, Applicative f) => (a -> f b) -> t a -> f b
foldMapA f = getAp . foldMap (Ap . f)

definitions of foldMap are usually defined to be advantageous over the structure of a particular Foldable; using foldr is picking the direction of association, which seems unnecessary.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions