Skip to content

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

Merged
merged 5 commits into from
Feb 10, 2022
Merged

Implement and test Foldable/Traversable instances #16

merged 5 commits into from
Feb 10, 2022

Conversation

MaybeJustJames
Copy link
Contributor

Implement a Foldable instance for Graph.


Checklist:

  • Added the change to the changelog's "Unreleased" section with a reference to this PR (e.g. "- Made a change (#0000)")
  • Linked any existing issues or proposals that this pull request should close
  • Updated or added relevant documentation
  • Added a test for the contribution (if applicable)

@thomashoneyman
Copy link
Member

Thanks! cc @colinwahl, as we were just working through this library together — do you have any thoughts?

@MaybeJustJames
Copy link
Contributor Author

MaybeJustJames commented Feb 10, 2022

I have a followup with Traversable here. Would you like it in the same PR?

@JordanMartinez
Copy link
Contributor

I'd say add it here.

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
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah... probably.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 20b2649

@JordanMartinez JordanMartinez changed the title Implement and test a Foldable instance Implement and test Foldable/Traversable instances Feb 10, 2022
@colinwahl
Copy link

@thomashoneyman Yep, looks fine to me!

@thomashoneyman thomashoneyman merged commit ac96114 into purescript:master Feb 10, 2022
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.

4 participants