-
Notifications
You must be signed in to change notification settings - Fork 10
Implement and test Foldable/Traversable instances #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement and test Foldable/Traversable instances #16
Conversation
Thanks! cc @colinwahl, as we were just working through this library together — do you have any thoughts? |
I have a followup with |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 m
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah... probably.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 20b2649
Thanks for help from @mikesol
@thomashoneyman Yep, looks fine to me! |
Implement a
Foldable
instance forGraph
.Checklist: