Skip to content

Commit

Permalink
Merge pull request #4558 from satorg/replace-pure-with-unit
Browse files Browse the repository at this point in the history
Replaces other `Applicative.pure(())` with `.unit`
  • Loading branch information
satorg authored Feb 4, 2024
2 parents f58181d + a97b299 commit 4f15360
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions free/src/test/scala/cats/free/FreeTSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class FreeTSuite extends CatsSuite {
checkAll("FreeT[Option, Option, Int", DeferTests[FreeTOption].defer[Int])

test("FlatMap stack safety tested with 50k flatMaps") {
val expected = Applicative[FreeTOption].pure(())
val expected = Applicative[FreeTOption].unit
val result =
Monad[FreeTOption].tailRecM(0)((i: Int) =>
if (i < 50000)
Expand All @@ -85,17 +85,17 @@ class FreeTSuite extends CatsSuite {
}

test("Stack safe with 50k left-associated flatMaps") {
val expected = Applicative[FreeTOption].pure(())
val expected = Applicative[FreeTOption].unit
val result =
(0 until 50000).foldLeft(Applicative[FreeTOption].pure(()))((fu, i) =>
(0 until 50000).foldLeft(Applicative[FreeTOption].unit)((fu, i) =>
fu.flatMap(u => Applicative[FreeTOption].pure(u))
)

assert(Eq[FreeTOption[Unit]].eqv(expected, result))
}

test("Stack safe with flatMap followed by 50k maps") {
val expected = Applicative[FreeTOption].pure(())
val expected = Applicative[FreeTOption].unit
val result =
(0 until 50000).foldLeft(().pure[FreeTOption].flatMap(_.pure[FreeTOption]))((fu, i) => fu.map(identity))

Expand All @@ -110,7 +110,7 @@ class FreeTSuite extends CatsSuite {
}

test("mapK stack-safety") {
val a = (0 until 50000).foldLeft(Applicative[FreeTOption].pure(()))((fu, i) =>
val a = (0 until 50000).foldLeft(Applicative[FreeTOption].unit)((fu, i) =>
fu.flatMap(u => Applicative[FreeTOption].pure(u))
)
val b = a.mapK(FunctionK.id)
Expand All @@ -126,7 +126,7 @@ class FreeTSuite extends CatsSuite {
}

test("compile stack-safety") {
val a = (0 until 50000).foldLeft(Applicative[FreeTOption].pure(()))((fu, i) =>
val a = (0 until 50000).foldLeft(Applicative[FreeTOption].unit)((fu, i) =>
fu.flatMap(u => Applicative[FreeTOption].pure(u))
)
val b = a.compile(FunctionK.id) // used to overflow
Expand All @@ -147,7 +147,7 @@ class FreeTSuite extends CatsSuite {
type F[A] = FreeT[Id, Option, A]
val F = MonadError[F, Unit]

val eff = F.flatMap(F.pure(()))(_ => F.raiseError[String](()))
val eff = F.flatMap(F.unit)(_ => F.raiseError[String](()))
assert(F.attempt(eff).runM(Some(_)) === Some(Left(())))
}

Expand Down
4 changes: 2 additions & 2 deletions laws/src/main/scala/cats/laws/ApplicativeLaws.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ trait ApplicativeLaws[F[_]] extends ApplyLaws[F] {
// Semigroupal's associativity law.

def monoidalLeftIdentity[A](fa: F[A]): (F[(Unit, A)], F[A]) =
(F.product(F.pure(()), fa), fa)
(F.product(F.unit, fa), fa)

def monoidalRightIdentity[A](fa: F[A]): (F[(A, Unit)], F[A]) =
(F.product(fa, F.pure(())), fa)
(F.product(fa, F.unit), fa)
}

object ApplicativeLaws {
Expand Down

0 comments on commit 4f15360

Please sign in to comment.