-
-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reexport Ap
and change definition of foldMapA
to use Ap
#121
Comments
this also applies to foldMapM |
This is seems like a great idea to me! |
note that i believe base 4.13 is going to have |
@chessai This looks like a good improvement! Unfortunately, |
Yeah. We could also define it for this purpose and not export it. What i typed out wrt Ap is all that's needed. |
@chessai Well, I'm actually okay with exporting And them |
Ap
and change definition of foldMapA
to use Ap
in that case, for GHC pre-8.6, you might want to just copy the code for Ap completely, and for base >= 4.12 re-export it. |
This is all the code related to -- | This data type witnesses the lifting of a 'Monoid' into an
-- 'Applicative' pointwise.
--
-- @since 4.12.0.0
newtype Ap f a = Ap { getAp :: f a }
deriving ( Alternative -- ^ @since 4.12.0.0
, Applicative -- ^ @since 4.12.0.0
, Enum -- ^ @since 4.12.0.0
, Eq -- ^ @since 4.12.0.0
, Functor -- ^ @since 4.12.0.0
, Generic -- ^ @since 4.12.0.0
, Generic1 -- ^ @since 4.12.0.0
, Monad -- ^ @since 4.12.0.0
, MonadFail -- ^ @since 4.12.0.0
, MonadPlus -- ^ @since 4.12.0.0
, Ord -- ^ @since 4.12.0.0
, Read -- ^ @since 4.12.0.0
, Show -- ^ @since 4.12.0.0
)
-- | @since 4.12.0.0
instance (Applicative f, Semigroup a) => Semigroup (Ap f a) where
(Ap x) <> (Ap y) = Ap $ liftA2 (<>) x y
-- | @since 4.12.0.0
instance (Applicative f, Monoid a) => Monoid (Ap f a) where
mempty = Ap $ pure mempty
-- | @since 4.12.0.0
instance (Applicative f, Bounded a) => Bounded (Ap f a) where
minBound = pure minBound
maxBound = pure maxBound
-- | @since 4.12.0.0
instance (Applicative f, Num a) => Num (Ap f a) where
(+) = liftA2 (+)
(*) = liftA2 (*)
negate = fmap negate
fromInteger = pure . fromInteger
abs = fmap abs
signum = fmap signum |
current definition of foldMapA:
instead of using an explicit right fold, it might be better to use whatever
foldMap
is, usingData.Monoid.Ap
: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.The text was updated successfully, but these errors were encountered: