Skip to content
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

foldMap, foldMap' for PrimArray #186

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
30 changes: 30 additions & 0 deletions Data/Primitive/PrimArray.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ module Data.Primitive.PrimArray
, foldlPrimArray
, foldlPrimArray'
, foldlPrimArrayM'
, foldrMapPrimArray
, foldlMapPrimArray
, foldrMapPrimArray'
, foldlMapPrimArray'
-- * Effectful Folding
, traversePrimArray_
, itraversePrimArray_
Expand Down Expand Up @@ -467,6 +471,32 @@ sizeofPrimArray :: forall a. Prim a => PrimArray a -> Int
{-# INLINE sizeofPrimArray #-}
sizeofPrimArray (PrimArray arr#) = I# (quotInt# (sizeofByteArray# arr#) (sizeOf# (undefined :: a)))

-- | Map each element of the primitive array to a monoid, and combine the results.
-- The combination is right-associated, and the accumulation is lazy.
chessai marked this conversation as resolved.
Show resolved Hide resolved
foldrMapPrimArray :: forall a m. (Prim a, Monoid m) => (a -> m) -> PrimArray a -> m
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather see the Map before the r.

{-# INLINE foldrMapPrimArray #-}
foldrMapPrimArray f = foldrPrimArray (\a acc -> f a `mappend` acc) mempty

-- | Map each element of the primitive array to a monoid, and combine the results.
-- The combination is left-associated, and the accumulation is lazy.
chessai marked this conversation as resolved.
Show resolved Hide resolved
foldlMapPrimArray :: forall a m. (Prim a, Monoid m) => (a -> m) -> PrimArray a -> m
{-# INLINE foldlMapPrimArray #-}
foldlMapPrimArray f = foldlPrimArray (\acc a -> acc `mappend` f a) mempty

-- | Map each element of the primitive array to a monoid, and combine the results.
-- The combination is right-associated, and the accumulation is strict, though
-- this function is not necessarily strict in its result.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"This function is not necessarily strict in its result" doesn't mean anything whatsoever to me. What I meant needed explanation is that at each step, we force the accumulator value, but we don't force the value of f a. So if mappend is lazy in its first argument, f a will not be evaluated. This is probably the correct behavior, but needs better explanation.

foldrMapPrimArray' :: forall a m. (Prim a, Monoid m) => (a -> m) -> PrimArray a -> m
{-# INLINE foldrMapPrimArray' #-}
foldrMapPrimArray' f = foldrPrimArray (\a !acc -> f a `mappend` acc) mempty

-- | Map each element of the primitive array to a monoid, and combine the results.
-- The combination is left-associated, and the accumulation is strict, though
-- this function is not necessarily strict in its result.
foldlMapPrimArray' :: forall a m. (Prim a, Monoid m) => (a -> m) -> PrimArray a -> m
{-# INLINE foldlMapPrimArray' #-}
foldlMapPrimArray' f = foldlPrimArray (\ !acc a -> acc `mappend` f a) mempty

-- | Lazy right-associated fold over the elements of a 'PrimArray'.
{-# INLINE foldrPrimArray #-}
foldrPrimArray :: forall a b. Prim a => (a -> b -> b) -> b -> PrimArray a -> b
Expand Down
4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@

## Changes in version 0.6.4.1

* Add instances for the following newtypes from `base`:
* Add `foldMapPrimArray` and `foldMapPrimArray'`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be updated now that you've added more functions.


* Add `Prim` instances for the following newtypes from `base`:
`Const`, `Identity`, `Down`, `Dual`, `Sum`, `Product`,
`First`, `Last`, `Min`, `Max`

Expand Down
2 changes: 1 addition & 1 deletion test/primitive-tests.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test-suite test
, tasty-quickcheck
, tagged
, transformers >= 0.3
, quickcheck-classes >= 0.4.11.1
, quickcheck-classes >= 0.4.14.2
ghc-options: -O2

source-repository head
Expand Down