Skip to content

Remove fold1 from Foldable1 #128

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 3 commits into from
Dec 29, 2020

Conversation

kl0tl
Copy link
Member

@kl0tl kl0tl commented Dec 13, 2020

Neither Foldable nor Bifoldable have a fold or a bifold method, is there any reason for Foldable1 to have fold1?

The only use of foldMap1Default in core libraries occurs in the Foldable1 instance for NonEmptyArray but this is easy enough to fix with a JavaScript implementation (like we do for foldr1 and foldl1), which should also be more efficient since it wouldn’t have to iterate through the array twice.

Close #125, close #117.

@hdgarrood
Copy link
Contributor

I'm not particularly keen on this personally, see discussion in #125

@hdgarrood
Copy link
Contributor

To reduce the amount of breakage outside of core / to make it easier to upgrade, could we rewrite foldMap1Default so that it’s based on foldl1 rather than removing it entirely?

@kl0tl
Copy link
Member Author

kl0tl commented Dec 27, 2020

We cannot implement foldMap1 with foldl1 because the accumulator and the values have to be of the same type.

@hdgarrood
Copy link
Contributor

How about adding a Functor constraint and using \f -> foldl1 (<>) <<< map f?

@kl0tl
Copy link
Member Author

kl0tl commented Dec 27, 2020

Oh right. I don’t think we should promote an inefficient default implementation of foldMap1 that has to traverse twice though, especially since Foldable1 instance will have to be updated anyway and that the existing implementation of fold1 can easily be turned into an implementation of foldMap1.

@hdgarrood
Copy link
Contributor

I think in many contexts, an extra traversal adds a (more or less) negligible performance cost, and having the default implementation there will make upgrading slightly less of a pain so I think I'd still prefer to keep it - especially since, as I just spotted, my proposed implementation is basically the same as the existing one. Most of this library is pretty poor performance-wise; if you care about performance you should probably avoid it and use specialized folds instead anyway.

Copy link
Contributor

@hdgarrood hdgarrood left a comment

Choose a reason for hiding this comment

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

Thank you!

@kl0tl
Copy link
Member Author

kl0tl commented Dec 27, 2020

Should we also bother with foldMap1DefaultL and foldMap1DefaultR default implementations (where foldMap1DefaultR is implemented with foldr1 and we alias foldMap1DefaultL to foldMap1Default) for consistency with Data.Foldable?

@hdgarrood
Copy link
Contributor

That works for me. Are you imagining putting a deprecation warning on foldMap1Default saying to use foldMap1DefaultL instead?

@kl0tl kl0tl force-pushed the remove-fold1-from-Foldable1 branch from 11bca07 to dc70cf3 Compare December 28, 2020 09:31
@kl0tl kl0tl force-pushed the remove-fold1-from-Foldable1 branch from dc70cf3 to 22a7587 Compare December 28, 2020 09:33
@JordanMartinez
Copy link
Contributor

To clarify, is this PR ready to be merged? I think all feedback has been addressed.

@thomashoneyman thomashoneyman merged commit b61328b into purescript:master Dec 29, 2020
@JordanMartinez
Copy link
Contributor

Looks like this PR broke these libraries:

  • tuples
  • lazy
  • free

I'll submit PRs removing the fold1 member from their Foldable1 instances.

@JordanMartinez
Copy link
Contributor

@kl0tl
Copy link
Member Author

kl0tl commented Dec 30, 2020

I opened purescript/purescript-ordered-collections#43 to fix the Foldable1 instance for NonEmptySet, it should be the last one in core!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants