Skip to content

Conversation

@Shimuuar
Copy link
Contributor

Fixes #263. foldMap is implemented in terms of foldr and foldMap' in terms of foldl'

This is draft since I want to make sure that we agree on implementation and documentation and only then do copy-paste work. I certainly don't like haddocks as wrote them. Those are riddles fit for sphinx. foldMap is useful when one do need laziness but I can't put in words. Suggestions?

@lehins
Copy link
Contributor

lehins commented Oct 27, 2020

Looks good to me. I don't think we need to dwell too much on explaining these functions since this is the concept from base. Maybe linking to corresponding functions would be enough for the unaware, eg "Same as 'Data.Foldable.foldMap', but for vectors"

@Shimuuar
Copy link
Contributor Author

I'd like to keep this PR for a few days. Maybe I'll come with something better. It's well known concept but I think it worth repeating

Data/Vector.hs Outdated
-- | @since NEXT
-- /O(n)/ Map each element of the structure to a monoid, and combine
-- the results. This function is implemented in terms of 'foldr' in
-- the same way as default implementation in 'Foldable' type class.
Copy link
Contributor

@Bodigrim Bodigrim Nov 21, 2020

Choose a reason for hiding this comment

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

We could avoid refering to implementation details by saying "Map each element of the structure to a monoid, and combine results {lazily,strictly}, similar to Data.Foldable.{foldMap,foldMap'}" or along these lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At first I removed foldl/foldr mentions as implementation detial. But then decided to restore it. Whether function will fuse or not depends whether foldl/foldr is used. It's unfortunate that implementation details leak like that

@Shimuuar Shimuuar marked this pull request as ready for review January 17, 2021 15:47
@Shimuuar Shimuuar force-pushed the foldMap branch 2 times, most recently from 6ba0ac5 to aed3022 Compare January 17, 2021 15:51
@Shimuuar
Copy link
Contributor Author

I tried and failed to come up with anything better. So I think PR is ready for merge

foldMap' f = foldl' (\acc a -> acc `mappend` f a) mempty



Copy link
Contributor

Choose a reason for hiding this comment

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

This is a very minor nitpicking, but you put three empty lines here, two empty lines in Data.Vector and one line elsewhere.

Co-authored-by: Bodigrim <andrew.lelechenko@gmail.com>
@Shimuuar
Copy link
Contributor Author

@Bodigrim fixed everything you mentioned

@Shimuuar Shimuuar merged commit 66d46ef into haskell:master Jan 24, 2021
@Shimuuar Shimuuar deleted the foldMap branch January 24, 2021 14:31
lehins pushed a commit that referenced this pull request Jan 26, 2021
Co-authored-by: Bodigrim <andrew.lelechenko@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No foldMap in Vector.Unboxed or Vector.Primitive

3 participants