Skip to content

fold, foldMap, foldl, & foldr#16

Open
robrix wants to merge 10 commits into
atzeus:masterfrom
robrix:foldmap
Open

fold, foldMap, foldl, & foldr#16
robrix wants to merge 10 commits into
atzeus:masterfrom
robrix:foldmap

Conversation

@robrix

@robrix robrix commented May 12, 2018

Copy link
Copy Markdown

This PR introduces tfold, tfoldMap, tfoldl, & tfoldr operations, by analogy with Foldable’s fold, foldMap, foldl, & foldr, but (in the case of tfold & tfoldMap) using a Category instance instead of a Monoid instance to combine elements.

As a fun aside, since all the various type-aligned sequence types have Category instances, we can use this to reconstruct one kind of sequence with another by using tfoldMap tsingleton. We can also do tfoldr (<|) tempty and tfoldl (|>) tempty, which is quite satisfying.

I think this resolves #4, tho I’d note that the types I arrived at differ from the ones mentioned here: https://stackoverflow.com/questions/30985070/how-can-i-express-foldr-in-terms-of-foldmap-for-type-aligned-sequences — in particular, the types mentioned there don’t admit the typical recursive definitions of left & right folds for ConsList, whereas the ones I’ve used do; and intuitively, tfoldr’s z parameter is its terminal value, whereas tfoldl’s z is its initial value.

I attempted to follow base’s definition of foldl with a Dual newtype, but I found that difficult; I think we would need Dual (Endo (Dual h)) rather than Dual (Endo h), tho I’m not at all certain of that. Nevertheless, I’m pretty satisfied with the result.

Thank you for your time & consideration!

@treeowl

treeowl commented Aug 3, 2021

Copy link
Copy Markdown
Collaborator

While you're waiting for the maintainer to respond, perhaps you should add tfoldl' and tfoldr'.

@treeowl

treeowl commented Aug 3, 2021

Copy link
Copy Markdown
Collaborator

I would also recommend extracting all this into a separate class, and making it a superclass of TASequence, despite the potential compatibility issues. It's also possible to write an analogue of Traversable.

@robrix

robrix commented Aug 10, 2021

Copy link
Copy Markdown
Author

I’m afraid I gave up on this somewhere around the second year.

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.

Add folds

2 participants