Skip to content

Commit

Permalink
Add Defer.fix (#3208)
Browse files Browse the repository at this point in the history
* Add Defer.fix

* format

* address Travis' comment
  • Loading branch information
johnynek authored and LukaJCB committed Dec 16, 2019
1 parent 02066f7 commit be23033
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
21 changes: 21 additions & 0 deletions core/src/main/scala/cats/Defer.scala
Expand Up @@ -28,6 +28,27 @@ package cats
*/
trait Defer[F[_]] extends Serializable {
def defer[A](fa: => F[A]): F[A]

/**
* Defer instances, like functions, parsers, generators, IO, etc...
* often are used in recursive settings where this function is useful
*
* fix(fn) == fn(fix(fn))
*
* example:
*
* val parser: P[Int] =
* Defer[P].fix[Int] { rec =>
* CharsIn("0123456789") | P("(") ~ rec ~ P(")")
* }
*
* Note, fn may not yield a terminating value in which case both
* of the above F[A] run forever.
*/
def fix[A](fn: F[A] => F[A]): F[A] = {
lazy val res: F[A] = defer(fn(res))
res
}
}

object Defer {
Expand Down
17 changes: 17 additions & 0 deletions tests/src/test/scala/cats/tests/FunctionSuite.scala
Expand Up @@ -23,6 +23,7 @@ import cats.laws.discipline.eq._
import cats.laws.discipline.arbitrary._
import cats.kernel.{CommutativeGroup, CommutativeMonoid, CommutativeSemigroup}
import cats.kernel.{Band, BoundedSemilattice, Semilattice}
import org.scalacheck.Gen

class FunctionSuite extends CatsSuite {

Expand All @@ -46,6 +47,22 @@ class FunctionSuite extends CatsSuite {
// TODO: make an binary compatible way to do this
// checkAll("Function1[Int => *]", DeferTests[Function1[Int, *]].defer[Int])

test("Defer[Function1[Int, *]].fix computing sum") {
val sum2 = Defer[Function1[Int, *]].fix[Int] {
rec =>
{ n: Int =>
if (n <= 0) 0 else n * n + rec(n - 1)
}
}

forAll(Gen.choose(0, 1000)) { n =>
// don't let n get too large because this consumes stack
assert(sum2(n) == (0 to n).map { n =>
n * n
}.sum)
}
}

checkAll("Semigroupal[Function1[Int, *]]", SerializableTests.serializable(Semigroupal[Function1[Int, *]]))

checkAll("Function1[MiniInt, Int]", MonadTests[MiniInt => *].monad[Int, Int, Int])
Expand Down

0 comments on commit be23033

Please sign in to comment.