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

Rename suspend to defer #1709

Merged
merged 1 commit into from
May 30, 2017
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
4 changes: 2 additions & 2 deletions bench/src/main/scala/cats/bench/TrampolineBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class TrampolineBench {

def trampolineFib(n: Int): Trampoline[Int] =
if (n < 2) Trampoline.done(n) else for {
x <- Trampoline.suspend(trampolineFib(n - 1))
y <- Trampoline.suspend(trampolineFib(n - 2))
x <- Trampoline.defer(trampolineFib(n - 1))
y <- Trampoline.defer(trampolineFib(n - 2))
} yield x + y

// TailRec[A] only has .flatMap in 2.11.
Expand Down
7 changes: 7 additions & 0 deletions free/src/main/scala/cats/free/Free.scala
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,14 @@ object Free {
/**
* Suspend the creation of a `Free[F, A]` value.
*/
@deprecated("Use Free.defer.", "1.0.0-MF")
def suspend[F[_], A](value: => Free[F, A]): Free[F, A] =
defer(value)

/**
* Defer the creation of a `Free[F, A]` value.
*/
def defer[F[_], A](value: => Free[F, A]): Free[F, A] =
pure(()).flatMap(_ => value)

/**
Expand Down
4 changes: 4 additions & 0 deletions free/src/main/scala/cats/free/FreeT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ object FreeT extends FreeTInstances {
/** Return the given value in the free monad. */
def pure[S[_], M[_], A](value: A)(implicit M: Applicative[M]): FreeT[S, M, A] = Suspend(M.pure(Right(value)))

@deprecated("Use FreeT.defer.", "1.0.0-MF")
def suspend[S[_], M[_], A](a: M[Either[A, S[FreeT[S, M, A]]]])(implicit M: Applicative[M]): FreeT[S, M, A] =
defer(a)

def defer[S[_], M[_], A](a: M[Either[A, S[FreeT[S, M, A]]]])(implicit M: Applicative[M]): FreeT[S, M, A] =
liftT(a).flatMap({
case Left(a) => pure(a)
case Right(s) => roll(s)
Expand Down
8 changes: 6 additions & 2 deletions free/src/main/scala/cats/free/Trampoline.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ private[free] abstract class TrampolineFunctions {
def done[A](a: A): Trampoline[A] =
Free.pure[Function0, A](a)

@deprecated("Use Trampoline.defer.", "1.0.0-MF")
def suspend[A](a: => Trampoline[A]): Trampoline[A] =
Free.suspend(a)
defer(a)

def defer[A](a: => Trampoline[A]): Trampoline[A] =
Free.defer(a)

def delay[A](a: => A): Trampoline[A] =
suspend(done(a))
defer(done(a))
}

8 changes: 4 additions & 4 deletions free/src/test/scala/cats/free/FreeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ class FreeTests extends CatsSuite {
}
}

test("suspend doesn't change value"){
test("defer doesn't change value"){
forAll { x: Free[List, Int] =>
Free.suspend(x) should === (x)
Free.defer(x) should === (x)
}
}

test("suspend is lazy"){
test("defer is lazy"){
def yikes[F[_], A]: Free[F, A] = throw new RuntimeException("blargh")
// this shouldn't throw an exception unless we try to run it
val _ = Free.suspend(yikes[Option, Int])
val _ = Free.defer(yikes[Option, Int])
}

test("compile consistent with foldMap"){
Expand Down