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 foldRightDefer to Foldable #2772

Merged
merged 8 commits into from
Nov 12, 2019
Merged

Add foldRightDefer to Foldable #2772

merged 8 commits into from
Nov 12, 2019

Conversation

denisrosca
Copy link
Contributor

This PR adds foldRightDefer to Foldable[F] with the following signature:

def foldRightDefer[G[_]: Defer, A, B](fa: F[A], gb: G[B])(fn: (A, G[B]) => G[B]): G[B]

A new law to check the foldRight <-> foldRightDefer equivalence where type G[A] = Eval[A] was also added to the test suite .

To avoid binary incompatibilities, I added the function as a syntax enrichment for Foldable[F] and F[A] given a Foldable[F].

Closes #2678

@kailuowang
Copy link
Contributor

Thanks very much!
It's a bit confusing, though, to have a law testing a method added through extension.
I see two options

  1. add the method to the type class directly and wait for the branch post 2.0 release
  2. comment out the law and test with a note that it's pending the method being moved into the type class post cats 2.0

I lean towards option 1 unless having this method is urgent to you.

@codecov-io
Copy link

codecov-io commented Apr 4, 2019

Codecov Report

Merging #2772 into master will decrease coverage by 0.02%.
The diff coverage is 75%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2772      +/-   ##
==========================================
- Coverage    93.3%   93.28%   -0.03%     
==========================================
  Files         376      376              
  Lines        7316     7323       +7     
  Branches      187      199      +12     
==========================================
+ Hits         6826     6831       +5     
- Misses        490      492       +2
Flag Coverage Δ
#scala_version_212 93.61% <75%> (-0.03%) ⬇️
#scala_version_213 90.91% <75%> (-0.04%) ⬇️
Impacted Files Coverage Δ
...ain/scala/cats/laws/discipline/FoldableTests.scala 100% <100%> (ø) ⬆️
laws/src/main/scala/cats/laws/FoldableLaws.scala 100% <100%> (ø) ⬆️
core/src/main/scala/cats/Foldable.scala 98.16% <50%> (-1.84%) ⬇️

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 f1ead7d...1cd2714. Read the comment docs.

@denisrosca
Copy link
Contributor Author

I don't really mind either way. I just thought you would prefer something that could be merged immediately.

I'll move the method to the typeclass.

@kailuowang kailuowang added this to the 2.1-RC1 milestone Apr 5, 2019
@kailuowang
Copy link
Contributor

Thanks very much!

def loop(it: Iterator[A]): Eval[B] =
Eval.defer(if (it.hasNext) f(it.next, loop(it)) else lb)
def iterateRight[A, B](iterable: Iterable[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
iterateRightDefer(iterable, lb)(f)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would imagine inlining Eval.defer will give a slight performance improvement since the jit can at best do this inlining later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@johnynek not sure I understand. Are you saying you would prefer the previous implementation explicitly calling Eval.defer?

@travisbrown
Copy link
Contributor

@denisrosca Do you mind merging master and changing the iterateRight implementation back to use Eval.defer directly as @johnynek suggested? Otherwise this looks good to me and it'd be nice to get it into 2.1.0. Thanks!

@denisrosca
Copy link
Contributor Author

@travisbrown Don't mind. Hopefully I'll get to it this week.

@travisbrown
Copy link
Contributor

@denisrosca Would you mind if I do it now and push to this PR branch? We're aiming to publish 2.1.0-RC1 on Friday and it'd be great to get this in.

@denisrosca
Copy link
Contributor Author

I don't mind if you prefer to do it now. Thanks.

Copy link
Contributor

@travisbrown travisbrown left a comment

Choose a reason for hiding this comment

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

@denisrosca Thanks, done! I'm happy to see this merged when green. @johnynek?

@LukaJCB LukaJCB merged commit ffded4b into typelevel:master Nov 12, 2019
@johnynek
Copy link
Contributor

Note, the addition of Iterable to Foldable in #2380 would allow us to use the iterateRightDefer method rather than the foldLeft implementation which I think will be more efficient (and I think allow short circuiting).

@johnynek
Copy link
Contributor

Thanks for adding this BTW!

gagandeepkalra added a commit to gagandeepkalra/cats that referenced this pull request Mar 1, 2020
travisbrown pushed a commit that referenced this pull request Mar 11, 2020
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.

Foldable could have foldRightDefer
6 participants