Optimise NonEmptyTraverse implementation#3382
Merged
travisbrown merged 4 commits intoJun 4, 2020
Merged
Conversation
diesalbla
reviewed
Apr 19, 2020
Comment on lines
+463
to
480
| tail.headOption.fold(Eval.now(Apply[G].map(f(head))(NonEmptyLazyList(_)))) { h => | ||
| Apply[G].map2Eval(f(head), Eval.defer(loop(h, tail.tail)))((b, acc) => b +: acc) | ||
| } |
Contributor
There was a problem hiding this comment.
There is a bit of LISP here. This code may be more readable as follows:
Suggested change
| tail.headOption.fold(Eval.now(Apply[G].map(f(head))(NonEmptyLazyList(_)))) { h => | |
| Apply[G].map2Eval(f(head), Eval.defer(loop(h, tail.tail)))((b, acc) => b +: acc) | |
| } | |
| val fh = f(head) | |
| tail match { | |
| case _ if tail.isEmpty => | |
| Eval.now(Apply[G].map(fh)(NonEmptyLazyList(_))) | |
| case h #:: ttail => | |
| val ftail = Eval.defer(loop(h, ttail)) | |
| Apply[G].map2Eval(fh, ftail)(_ +: _) | |
| } |
The changes are:
- Extract
f(head), which is eagerly evaluated in both branches, as aval. - Replace the
Option.foldby a pattern-match. - Replace the cases of
headOptionby two cases on thetaillazy list itself. For the first case, there seems not to be any case-object, likeNil, for the empty list. Discussion. - In the second case, abbreviate
(b, acc) => b +: accby underscores_ +: _.
Contributor
Author
There was a problem hiding this comment.
Hello, I see what you mean there.
On my part, usually I try to avoid pattern match (it's slow)
9c6e999 to
fbecfba
Compare
Codecov Report
@@ Coverage Diff @@
## master #3382 +/- ##
==========================================
- Coverage 91.63% 91.60% -0.03%
==========================================
Files 382 382
Lines 8304 8311 +7
Branches 232 226 -6
==========================================
+ Hits 7609 7613 +4
- Misses 695 698 +3 |
travisbrown
previously approved these changes
Jun 2, 2020
Contributor
travisbrown
left a comment
There was a problem hiding this comment.
This looks good to me! Do you mind rebasing to resolve the conflicts, @gagandeepkalra? I can merge it after that.
fbecfba to
dd3c3bc
Compare
6dbb35a to
50a0cf9
Compare
Contributor
Author
|
Thank you @travisbrown @LukaJCB, this was fun. I'm happy knowing short-circuiting is already useful (I see foldable there). |
travisbrown
approved these changes
Jun 4, 2020
Contributor
travisbrown
left a comment
There was a problem hiding this comment.
Thanks again, @gagandeepkalra!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
resolves #3381
depends on #3375