Skip to content
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

Add foldl and foldr aliases to Foldable #2020

Merged
merged 3 commits into from
Nov 9, 2017

Conversation

felixmulder
Copy link
Contributor

@felixmulder felixmulder commented Nov 7, 2017

This PR adds aliases for Foldable's foldRight and foldLeft ops so that the callee can be sure that the correct method is being invoked.

Because of syntax extensions the end signature looks like:

def foldRight[B](b: Eval[B])(f: (Eval[B], A) => Eval[B]): Eval[B]

This can then lead to some confusion when folding over things which already have the foldRight operation, namely pretty much all collections.

Even though we do:

import cats.implicits._

List
  .range(0, 10000000)
  .foldRight(Eval.now(0))((n, e) => e.map(_ + n))

the fold won't be evaluated lazily, using Foldable's definition of foldRight instead the stdlib's version will be chosen.

To avoid this pitfall, this PR introduces these aliases:

  • foldr <=> foldRight
  • foldl <=> foldLeft

these methods can be used safely with syntax extensions.

@kailuowang
Copy link
Contributor

seems you were on a code base prior to 1.0.0-MF. These alias should probably be added to the type class , but they will break bin compat with 1.0RC1 so it would be helpful if there are more justification in the PR.

@felixmulder
Copy link
Contributor Author

Sure, I'll update the initial comment with a more detailed explanation.

@kailuowang
Copy link
Contributor

as expected, this fails the binary compat check

Cats core: found 1 potential binary incompatibilities while checking against org.typelevel:cats-core_2.10:1.0.0-RC1

  • method catsSyntaxFoldOps(java.lang.Object,cats.Foldable)java.lang.Object in trait cats.syntax.FoldableSyntax is present only in current version
    filter with: ProblemFilters.exclude[ReversedMissingMethodProblem("cats.syntax.FoldableSyntax.catsSyntaxFoldOps")�

we can add an exception filter here
Here is the documentation on how
please also add a comment that this filter shall be removed post 1.0 release.

@felixmulder
Copy link
Contributor Author

Cool, thanks for having a look, will update this tomorrow 👍

@codecov-io
Copy link

codecov-io commented Nov 8, 2017

Codecov Report

Merging #2020 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2020      +/-   ##
==========================================
+ Coverage   95.08%   95.08%   +<.01%     
==========================================
  Files         301      301              
  Lines        4943     4946       +3     
  Branches      124      125       +1     
==========================================
+ Hits         4700     4703       +3     
  Misses        243      243
Impacted Files Coverage Δ
core/src/main/scala/cats/Foldable.scala 100% <ø> (ø) ⬆️
core/src/main/scala/cats/syntax/foldable.scala 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6652845...07a1dbb. Read the comment docs.

@felixmulder
Copy link
Contributor Author

@kailuowang - since the syntax suite doesn't actually run, I guess codecov doesn't pick it up. Is there somewhere I should add a runtime test for this so as not to affect codecov?

@kailuowang
Copy link
Contributor

@felixmulder a good way to add test coverage for such aliases is doctest. How about we mention these aliases and provide some doctests in scala docs for foldRight and foldLeft in Foldable. Examples of doctests can be found also in the file here

@felixmulder
Copy link
Contributor Author

@kailuowang - added the doctest 👍

Copy link
Contributor

@kailuowang kailuowang left a comment

Choose a reason for hiding this comment

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

Awesome! Thanks very much!

@kailuowang kailuowang merged commit 6863f3f into typelevel:master Nov 9, 2017
@felixmulder felixmulder deleted the topic/add-foldl-foldr branch November 9, 2017 12:44
@felixmulder
Copy link
Contributor Author

NP, thanks for the pointers :)

@kailuowang kailuowang added this to the 1.0.0 milestone Nov 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants