Implement and test Foldable/Traversable instances#16
Merged
thomashoneyman merged 5 commits intopurescript:masterfrom Feb 10, 2022
MaybeJustJames:foldable-instance
Merged
Implement and test Foldable/Traversable instances#16thomashoneyman merged 5 commits intopurescript:masterfrom MaybeJustJames:foldable-instance
thomashoneyman merged 5 commits intopurescript:masterfrom
MaybeJustJames:foldable-instance
Conversation
Member
|
Thanks! cc @colinwahl, as we were just working through this library together — do you have any thoughts? |
Contributor
Author
|
I have a followup with |
JordanMartinez
approved these changes
Feb 10, 2022
Contributor
|
I'd say add it here. |
src/Data/Graph.purs
Outdated
| instance foldableGraph :: Foldable (Graph k) where | ||
| foldl f z (Graph m) = foldl f z $ fst <$> M.values m | ||
| foldr f z (Graph m) = foldr f z $ fst <$> M.values m | ||
| foldMap f (Graph m) = foldMap f $ fst <$> M.values m |
Contributor
There was a problem hiding this comment.
Ideally, we would use 1 fold rather than 2 here (i.e. M.values folds once, then <$> folds a second time). But the single fold version...
foldl f z (foldl (\acc (Tuple k _) -> Array.snoc acc k) [] m)
... is likely less performant because of the usage of Array.snoc.
Contributor
Author
There was a problem hiding this comment.
Would something like this be better?
instance foldableGraph :: Foldable (Graph k) where
foldl f z (Graph m) = foldl (\acc (Tuple k _) -> f acc k) z $ M.values m
foldr f z (Graph m) = foldr (\(Tuple k _) acc -> f k acc) z $ M.values m
foldMap f (Graph m) = foldMap (f <<< fst) $ M.values mThanks for help from @mikesol
JordanMartinez
approved these changes
Feb 10, 2022
|
@thomashoneyman Yep, looks fine to me! |
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.
Implement a
Foldableinstance forGraph.Checklist: