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

Backport #2380 Add combineAllOption to Foldable #3340

Conversation

gagandeepkalra
Copy link
Contributor

@gagandeepkalra gagandeepkalra commented Mar 2, 2020

addresses #3143

@codecov-io
Copy link

codecov-io commented Mar 2, 2020

Codecov Report

Merging #3340 into scala_2.11 will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@              Coverage Diff               @@
##           scala_2.11    #3340      +/-   ##
==============================================
+ Coverage       93.41%   93.42%   +<.01%     
==============================================
  Files             389      389              
  Lines            7294     7295       +1     
  Branches          187      188       +1     
==============================================
+ Hits             6814     6815       +1     
  Misses            480      480
Impacted Files Coverage Δ
core/src/main/scala/cats/Foldable.scala 98.05% <100%> (-0.02%) ⬇️
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 f82cd0d...41f6306. Read the comment docs.

@travisbrown
Copy link
Contributor

There's no need for FoldableCompat when building only for 2.11 because Stream isn't deprecated until 2.13, so I think it'd be better to implement this directly in the 2.11 branch.

@travisbrown travisbrown self-requested a review March 5, 2020 11:12
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.

Looks good to me!

* not to force a full materialization of the collection.
*/
def toIterable(implicit F: Foldable[F]): Iterable[A] =
F.foldRight[A, Stream[A]](fa, Eval.now(Stream.empty))((a, eb) => eb.map(Stream.cons(a, _))).value
Copy link
Contributor Author

@gagandeepkalra gagandeepkalra Mar 5, 2020

Choose a reason for hiding this comment

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

@travisbrown

Hello, sorry to bother you again on this PR.

I was looking at the toIterable implementation to understand why would we use Stream.

These are the possible arguments that I read from the original PR-

Still aiming at mitigating the possible cost of building a list, it could be delayed up to when the iterator is traversing it

and

@travisbrown .toList.iterator forces a full materialization, and we don't control the API of List. So, the uses of a lazy structure (like Iterator, Stream, LazyList) are very different since we can interact with other tools that will not force a full materialization of the data. This is useful in things like spark or scalding.

That said, I think in a library focused on FP, we should use an immutable lazy structure if possible, so I would lean towards Stream/LazyList. Users can then call .iterator on that, but we don't control that API. Alternatively, they can use foldLeft to tear down the Stream without materializing the whole thing in memory at once.

this is when I added a few printLn in between, confirming my suspicion that the Stream is created from the end to the first element, this means that the entire Stream is already materialised yet not visible.

Screenshot 2020-03-05 at 9 09 28 PM

when replacing Stream with List, no difference.

Screenshot 2020-03-05 at 9 16 09 PM

and given this evidence we can replace this entire implementation with-

F.foldLeft(fa, List.empty[A])((acc, a) => a :: acc).reverse

Stream is materialised yet not visible then re-materialised (going through the whole synchronise thing).

Do you see value here?

Copy link
Contributor Author

@gagandeepkalra gagandeepkalra Mar 11, 2020

Choose a reason for hiding this comment

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

@travisbrown if you get a chance to go through this one, that's the last of it

@travisbrown travisbrown merged commit 40544c9 into typelevel:scala_2.11 Mar 11, 2020
@gagandeepkalra gagandeepkalra deleted the backport/foldable/combineAllOption branch March 12, 2020 12:42
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.

3 participants