Open
Conversation
This uses an approach due to Joachim Breitner (https://stackoverflow.com/a/30986119/88018) mirroring the definition of foldr given in base using the Endo Monoid.
…t-associated folds over sequences.
Collaborator
|
While you're waiting for the maintainer to respond, perhaps you should add |
Collaborator
|
I would also recommend extracting all this into a separate class, and making it a superclass of |
Author
|
I’m afraid I gave up on this somewhere around the second year. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces
tfold,tfoldMap,tfoldl, &tfoldroperations, by analogy withFoldable’sfold,foldMap,foldl, &foldr, but (in the case oftfold&tfoldMap) using aCategoryinstance instead of aMonoidinstance to combine elements.As a fun aside, since all the various type-aligned sequence types have
Categoryinstances, we can use this to reconstruct one kind of sequence with another by usingtfoldMap tsingleton. We can also dotfoldr (<|) temptyandtfoldl (|>) 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’szparameter is its terminal value, whereastfoldl’szis its initial value.I attempted to follow
base’s definition offoldlwith aDualnewtype, but I found that difficult; I think we would needDual (Endo (Dual h))rather thanDual (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!