-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Optimize foldMap implementations with combineAll #2024
Conversation
007963d
to
90982b9
Compare
Codecov Report
@@ Coverage Diff @@
## master #2024 +/- ##
==========================================
+ Coverage 95.08% 95.09% +<.01%
==========================================
Files 301 301
Lines 4946 4953 +7
Branches 125 117 -8
==========================================
+ Hits 4703 4710 +7
Misses 243 243
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! One question about the Set foldMap which I think should also use .iterator
.
@@ -68,6 +68,9 @@ object SetInstances { | |||
def foldRight[A, B](fa: Set[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = | |||
Foldable.iterateRight(fa, lb)(f) | |||
|
|||
override def foldMap[A, B](fa: Set[A])(f: A => B)(implicit B: Monoid[B]): B = | |||
B.combineAll(fa.map(f)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fa.iterator.map(f)
will be faster (not allocate a set) also maybe more correct. fa.map(f)
if f = { _ => 1 }
will create a set of 1 thing. If you do foldMap
what do we mean? I actually don't know if this should return the count of items (which I think it should, which means we should do fa.iterator.map
), or if it should return 0 if empty and 1 otherwise.
I guess since this is lawless especially we need a comment as to the behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point, I originally avoided using .iterator
to maintain the same semantics, but I forgot to confirm that was the case.
@ alleycats.std.SetInstances.setTraverse.foldMap(Set(1,2,3))(_ => "a")
res13: String = "aaa"
Seems not using .iterator
actually would be a breaking change, so I'll switch to use it. Nice catch!
Do you guys mind to provide some enlightenment here? I must've missed something, I have trouble understanding how this optimize over the default implementation. |
|
thanks @tpolecat . |
90982b9
to
eb15caa
Compare
👍 |
No description provided.