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

Make MonoidK[Endo] stack safe on combineK #2760

Merged
merged 1 commit into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions core/src/main/scala/cats/instances/function.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package instances

import cats.Contravariant
import cats.arrow.{ArrowChoice, Category, CommutativeArrow}
import cats.data.AndThen

import annotation.tailrec

Expand Down Expand Up @@ -151,8 +152,15 @@ sealed private[instances] trait Function1Instances extends Function1Instances0 {
def compose[A, B, C](f: B => C, g: A => B): A => C = f.compose(g)
}

implicit val catsStdMonoidKForFunction1: MonoidK[Endo] =
Category[Function1].algebraK
implicit val catsStdMonoidKForFunction1: MonoidK[Endo] = new MonoidK[Endo] {

val category: Category[Function] = Category[Function1]

override def empty[A]: Endo[A] = category.id

override def combineK[A](x: Endo[A], y: Endo[A]): Endo[A] =
AndThen(category.compose(x, y))
}

}

Expand Down
8 changes: 8 additions & 0 deletions tests/src/test/scala/cats/tests/FunctionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,12 @@ class FunctionSuite extends CatsSuite {
checkAll("CommutativeGroup[String => CGrp]", SerializableTests.serializable(CommutativeGroup[String => CGrp]))
checkAll("ContravariantMonoidal[Function1[?, Monoid]]",
SerializableTests.serializable(ContravariantMonoidal[? => Long]))

test("MonoidK[Endo] is stack safe on combineK") {
def incrementAll(as: Int): Int = as + 1
val bigList: List[Int => Int] = List.fill(50000)(incrementAll)

val sumAll = bigList.combineAll(MonoidK[Endo].algebra)
List(1, 1, 1).map(sumAll)
}
}