primitive icon indicating copy to clipboard operation
primitive copied to clipboard

foldMap, foldMap' for PrimArray

Open chessai opened this issue 7 years ago • 19 comments

chessai avatar Jul 15 '18 00:07 chessai

I'm good with this. If there aren't any objection in the next few days, I'll merge this.

andrewthad avatar Jul 16 '18 17:07 andrewthad

No objections here, although these deserve to be mentioned in the changelog.

RyanGlScott avatar Jul 16 '18 18:07 RyanGlScott

Sorry I keep forgetting that. I'll add it in.

chessai avatar Jul 16 '18 19:07 chessai

Is this good to be merged? The travis failure is unrelated.

chessai avatar Sep 17 '18 18:09 chessai

Sorry for the delayed review

treeowl avatar Sep 17 '18 19:09 treeowl

That is okay, this is valuable feedback. I will make these changes shortly.

chessai avatar Sep 17 '18 19:09 chessai

most recent commit does not add balanced foldMap. I think I'd prefer for that to be in a separate PR, as it's a little more complicated.

chessai avatar Sep 17 '18 20:09 chessai

requested changes attempted, @treeowl

chessai avatar Sep 18 '18 01:09 chessai

Both examples are supposed to show associativity. I think they can be much briefer, TBH. Just give a three-element example application and show how it unfolds as a single expression.

On Mon, Sep 17, 2018, 9:54 PM chessai [email protected] wrote:

@chessai commented on this pull request.

In Data/Primitive/PrimArray.hs https://github.com/haskell/primitive/pull/186#discussion_r218279364:

+-- +-- @mySum = 'Data.Monoid.getSum' '$' ('Data.Monoid.Sum' 1 '<>' ('Data.Monoid.Sum' 2 '<>' ('Data.Monoid.Sum' 3 '<>' 'Data.Monoid.Sum' 4)))@ +-- +-- @mySum = 'Data.Monoid.getSum' '$' ('Data.Monoid.Sum' 1 '<>' ('Data.Monoid.Sum' 2 '<>' ('Data.Monoid.Sum' 7)))@ +-- +-- @mySum = 'Data.Monoid.getSum' '$' ('Data.Monoid.Sum' 1 '<>' ('Data.Monoid.Sum' 9)@ +-- +-- @mySum = 'Data.Monoid.getSum' '$' 'Data.Monoid.Sum' 10@ +-- +-- @mySum = 10@ +foldMapRPrimArray :: forall a m. (Prim a, Monoid m) => (a -> m) -> PrimArray a -> m +{-# INLINE foldMapRPrimArray #-} +foldMapRPrimArray 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.

Clarification: Are you asking to provide an example for foldMapLPrimArray, to show left-associativity (for this particular lazy fold)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/haskell/primitive/pull/186#discussion_r218279364, or mute the thread https://github.com/notifications/unsubscribe-auth/ABzi_V_eja4orM-34lJJs1TwInJE6ERrks5ucFJVgaJpZM4VQDt8 .

treeowl avatar Sep 18 '18 02:09 treeowl

Both examples are supposed to show associativity. I think they can be much briefer, TBH. Just give a three-element example application and show how it unfolds as a single expression.

By 'briefer', do you mean less 'tutorial-like', to assume that the example is illustrative enough sans written explanation?

chessai avatar Sep 18 '18 02:09 chessai

By 'briefer', do you mean less 'tutorial-like', to assume that the example is illustrative enough sans written explanation?

Right. This is primitive, not base or containers. Users are expected to know what they're doing. But language is slippery, so "associates to the left" might well evoke opposite meanings in the minds of two different people.

treeowl avatar Sep 18 '18 02:09 treeowl

Right. This is primitive, not base or containers. Users are expected to know what they're doing. But language is slippery, so "associates to the left" might well evoke opposite meanings in the minds of two different people.

Makes sense - understood

chessai avatar Sep 18 '18 02:09 chessai

Aha! I've changed my mind :). I believe we should force each function value in the strict versions. Laziness here is kind of silly, but more to the point, I believe the laziness reveals semantic differences in fold associativity that are only supposed to be performance differences.

On Mon, Sep 17, 2018, 10:17 PM chessai [email protected] wrote:

Right. This is primitive, not base or containers. Users are expected to know what they're doing. But language is slippery, so "associates to the left" might well evoke opposite meanings in the minds of two different people.

Makes sense - understood

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/haskell/primitive/pull/186#issuecomment-422230980, or mute the thread https://github.com/notifications/unsubscribe-auth/ABzi_a3CzXzv1s-TcI2ZeePIIZX1mptbks5ucFe5gaJpZM4VQDt8 .

treeowl avatar Sep 18 '18 02:09 treeowl

@treeowl Examples are now simplified and function values are being forced

chessai avatar Sep 18 '18 02:09 chessai

Humor me and check whether what I said about forcing is really true. I was going off intuition.

On Mon, Sep 17, 2018, 10:53 PM chessai [email protected] wrote:

@treeowl https://github.com/treeowl Examples are now simplified and function values are being forced

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/haskell/primitive/pull/186#issuecomment-422236769, or mute the thread https://github.com/notifications/unsubscribe-auth/ABzi_VlFO4Cj6buIYDs2COP8-OJAPoAPks5ucGASgaJpZM4VQDt8 .

treeowl avatar Sep 18 '18 02:09 treeowl

OK

chessai avatar Sep 18 '18 14:09 chessai

I like the stuff in the PR, although I dislike the naming convention that @treeowl suggested. I actually prefer foldrMapPrimArray to foldMapRPrimArray. Anyone else got any thoughts on the color of the bikeshed?

andrewthad avatar Jun 12 '20 19:06 andrewthad

I would still like this and also a foldl for ByteArray at some point. Happy to help to make that happen in whatever way I can.

Boarders avatar Nov 24 '20 21:11 Boarders

I'll change the names and merge this. @Boarders Open a separate PR for a foldl on ByteArray. You should be able to nearly copy and paste the foldl on PrimArray.

andrewthad avatar Nov 25 '20 13:11 andrewthad