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

Return stack-safe Function0 and Function1 from Semigroup#combine #4093

Merged
merged 4 commits into from
Dec 23, 2021
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cats.kernel
package instances

import cats.kernel.compat.scalaVersionSpecific._
import scala.util.control.TailCalls.{done, tailcall, TailRec}

@suppressUnusedImportWarningForScalaVersionSpecific
trait FunctionInstances extends FunctionInstances0 {

implicit def catsKernelOrderForFunction0[A](implicit ev: Order[A]): Order[() => A] =
Expand Down Expand Up @@ -106,11 +110,33 @@ private[instances] trait FunctionInstances4 {
new Function1Semigroup[A, B] { def B: Semigroup[B] = S }
}

final private[instances] case class CombineFunction1[A, B](left: A => B, right: A => B, semiB: Semigroup[B])
extends (A => B) {
private def call(fn: A => B, a: A): TailRec[B] =
fn match {
case ref: CombineFunction1[A, B] @unchecked =>
for {
lb <- tailcall(call(ref.left, a))
rb <- tailcall(call(ref.right, a))
} yield ref.semiB.combine(lb, rb)
case _ => done(fn(a))
}

final override def apply(a: A): B = call(this, a).result
}

trait Function1Semigroup[A, B] extends Semigroup[A => B] {
implicit def B: Semigroup[B]

override def combine(x: A => B, y: A => B): A => B =
(a: A) => B.combine(x(a), y(a))
CombineFunction1(x, y, B)

override def combineAllOption(fns: IterableOnce[A => B]): Option[A => B] =
if (fns.iterator.isEmpty) None
else
Some { (a: A) =>
B.combineAllOption(fns.iterator.map(_.apply(a))).get
}
}
mrdziuban marked this conversation as resolved.
Show resolved Hide resolved

trait Function1Monoid[A, B] extends Function1Semigroup[A, B] with Monoid[A => B] {
Expand All @@ -127,11 +153,33 @@ trait Function1Group[A, B] extends Function1Monoid[A, B] with Group[A => B] {
(a: A) => B.inverse(x(a))
}

final private[instances] case class CombineFunction0[A](left: () => A, right: () => A, semiA: Semigroup[A])
extends (() => A) {
private def call(fn: () => A): TailRec[A] =
fn match {
case ref: CombineFunction0[A] @unchecked =>
for {
la <- tailcall(call(ref.left))
ra <- tailcall(call(ref.right))
} yield ref.semiA.combine(la, ra)
case _ => done(fn())
}

final override def apply(): A = call(this).result
}

trait Function0Semigroup[A] extends Semigroup[() => A] {
implicit def A: Semigroup[A]

override def combine(x: () => A, y: () => A): () => A =
() => A.combine(x(), y())
CombineFunction0(x, y, A)

override def combineAllOption(fns: IterableOnce[() => A]): Option[() => A] =
if (fns.iterator.isEmpty) None
else
Some { () =>
A.combineAllOption(fns.iterator.map(_.apply())).get
}
}
mrdziuban marked this conversation as resolved.
Show resolved Hide resolved

trait Function0Monoid[A] extends Function0Semigroup[A] with Monoid[() => A] {
Expand Down
14 changes: 14 additions & 0 deletions tests/src/test/scala/cats/tests/FunctionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,18 @@ class FunctionSuite extends CatsSuite {
val sumAll = bigList.combineAll(MonoidK[Endo].algebra)
List(1, 1, 1).map(sumAll)
}

property("Semigroup[Function0[Semi]] is stack safe on combine") {
forAll { (f: Function0[Semi]) =>
1.to(50000).foldLeft(f)((acc, _) => Semigroup[Function0[Semi]].combine(acc, f)).apply()
true
}
}

property("Semigroup[Function1[MiniInt, Semi]] is stack safe on combine") {
forAll { (i: MiniInt, f: Function1[MiniInt, Semi]) =>
1.to(50000).foldLeft(f)((acc, _) => Semigroup[Function1[MiniInt, Semi]].combine(acc, f)).apply(i)
true
}
}
}