Skip to content

Commit

Permalink
Reinstate @johnynek's optimization from typelevel#3279
Browse files Browse the repository at this point in the history
  • Loading branch information
travisbrown committed Feb 26, 2020
1 parent 2e12f4d commit 4b643d8
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions kernel/src/main/scala/cats/kernel/CommutativeSemigroup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ trait CommutativeSemigroup[@sp(Int, Long, Float, Double) A] extends Any with Sem
new CommutativeSemigroup[A] {
def combine(a: A, b: A): A =
self.combine(a, self.combine(middle, b))

override def combineN(a: A, n: Int): A =
if (n <= 1) self.combineN(a, n)
else {
// a + m + a ... = combineN(a, n) + combineN(m, n - 1)
self.combine(self.combineN(a, n), self.combineN(middle, n - 1))
}
}
}

Expand Down

0 comments on commit 4b643d8

Please sign in to comment.