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

Add Defer.fix #3208

Merged
merged 3 commits into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 21 additions & 0 deletions core/src/main/scala/cats/Defer.scala
Original file line number Diff line number Diff line change
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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is rather nice! I never knew I wanted this.

}

object Defer {
Expand Down
16 changes: 16 additions & 0 deletions tests/src/test/scala/cats/tests/FunctionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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 { n0: Int =>
val n = n0 & 0x3FF // don't let it get too big
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nitpicky but could you just write this out for clarity? I can't remember off the top of my head what this does for negative n0 for example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's taking the 18 least significant bits. Perhaps a slightly clearer way to write it for those who aren't doing bit manipulation on a regular basis would be math.abs(n0 % 262144).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to be more direct Gen.choose(0, 1000) which I think is nicer anyway. Note 0x3ff = 1024, it is actually 8 + 2 bits.

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