From 0efe89ca4d126010454d3b1f55df063231d3f1a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Piaggio?= Date: Thu, 16 Nov 2023 13:45:05 -0300 Subject: [PATCH] cogens. better pot conversions. --- build.sbt | 2 +- .../shared/src/main/scala/crystal/Pot.scala | 2 + .../src/main/scala/crystal/PotOption.scala | 7 ++- .../main/scala/crystal/arb/arbitraries.scala | 45 +++++++++---------- .../test/scala/crystal/PotOptionSpec.scala | 4 +- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/build.sbt b/build.sbt index ad856d05..de4789ad 100644 --- a/build.sbt +++ b/build.sbt @@ -1,7 +1,7 @@ Global / onChangedBuildSource := ReloadOnSourceChanges ThisBuild / crossScalaVersions := List("3.3.1") -ThisBuild / tlBaseVersion := "0.36" +ThisBuild / tlBaseVersion := "0.37" ThisBuild / tlCiReleaseBranches := Seq("master") diff --git a/modules/core/shared/src/main/scala/crystal/Pot.scala b/modules/core/shared/src/main/scala/crystal/Pot.scala index 44ce4099..50ec1360 100644 --- a/modules/core/shared/src/main/scala/crystal/Pot.scala +++ b/modules/core/shared/src/main/scala/crystal/Pot.scala @@ -64,6 +64,8 @@ sealed trait Pot[+A] { case Pot.Error(t) => Failure(t).some case Pot.Ready(a) => Success(a).some } + + def toOptionEither: Option[Either[Throwable, A]] = toOptionTry.map(_.toEither) } object Pot { diff --git a/modules/core/shared/src/main/scala/crystal/PotOption.scala b/modules/core/shared/src/main/scala/crystal/PotOption.scala index 963cfaeb..f672cf18 100644 --- a/modules/core/shared/src/main/scala/crystal/PotOption.scala +++ b/modules/core/shared/src/main/scala/crystal/PotOption.scala @@ -61,8 +61,11 @@ sealed trait PotOption[+A] { def toOption: Option[A] = fold(none, _ => none, none, _.some) - def toOptionTry: Option[Try[A]] = - toPot.toOptionTry + def toOptionTry: Option[Try[Option[A]]] = + fold(none, t => Failure(t).some, Success(none).some, a => Success(a.some).some) + + def toOptionEither: Option[Either[Throwable, Option[A]]] = + toOptionTry.map(_.toEither) } object PotOption { diff --git a/modules/testkit/shared/src/main/scala/crystal/arb/arbitraries.scala b/modules/testkit/shared/src/main/scala/crystal/arb/arbitraries.scala index fd2acad8..358b8c13 100644 --- a/modules/testkit/shared/src/main/scala/crystal/arb/arbitraries.scala +++ b/modules/testkit/shared/src/main/scala/crystal/arb/arbitraries.scala @@ -9,37 +9,32 @@ import crystal.PotOption import crystal.ViewF import org.scalacheck.* -import scala.annotation.targetName - import Arbitrary.* import Gen.* -given [A: Arbitrary]: Arbitrary[Pot[A]] = - Arbitrary( - oneOf( - Gen.const(Pot.Pending), - arbitrary[Throwable].map(Pot.Error.apply), - arbitrary[A].map(Pot.Ready.apply) - ) +given [A: Arbitrary]: Arbitrary[Pot[A]] = Arbitrary: + oneOf( + Gen.const(Pot.Pending), + arbitrary[Throwable].map(Pot.Error.apply), + arbitrary[A].map(Pot.Ready.apply) ) -given [A](using Arbitrary[A => A]): Arbitrary[Pot[A] => Pot[A]] = - Arbitrary(arbitrary[A => A].map(f => _.map(f))) - -given [A: Arbitrary]: Arbitrary[PotOption[A]] = - Arbitrary( - oneOf( - Gen.const(PotOption.Pending), - arbitrary[Throwable].map(PotOption.Error.apply), - Gen.const(PotOption.ReadyNone), - arbitrary[A].map(PotOption.ReadySome.apply) - ) +given [A: Cogen]: Cogen[Pot[A]] = + Cogen[Option[Option[A]]].contramap(_.toOptionTry.map(_.toOption)) + +given [A: Arbitrary]: Arbitrary[PotOption[A]] = Arbitrary: + oneOf( + Gen.const(PotOption.Pending), + arbitrary[Throwable].map(PotOption.Error.apply), + Gen.const(PotOption.ReadyNone), + arbitrary[A].map(PotOption.ReadySome.apply) ) -given viewFArb[A: Arbitrary]: Arbitrary[ViewF[Id, A]] = Arbitrary( +given [A: Cogen]: Cogen[PotOption[A]] = + Cogen[Option[Option[Option[A]]]].contramap(_.toOptionTry.map(_.toOption)) + +given viewFArb[A: Arbitrary]: Arbitrary[ViewF[Id, A]] = Arbitrary: arbitrary[A].map(a => ViewF.apply[Id, A](a, (f, cb) => cb(f(a)))) -) -@targetName("potOptionFnArbitrary") -given [A](using fArb: Arbitrary[A => A]): Arbitrary[PotOption[A] => PotOption[A]] = - Arbitrary(arbitrary[A => A].map(f => _.map(f))) +given [A: Cogen]: Cogen[ViewF[Id, A]] = + Cogen[A].contramap(_.get) diff --git a/modules/tests/shared/src/test/scala/crystal/PotOptionSpec.scala b/modules/tests/shared/src/test/scala/crystal/PotOptionSpec.scala index 935ad8ef..b5089a3b 100644 --- a/modules/tests/shared/src/test/scala/crystal/PotOptionSpec.scala +++ b/modules/tests/shared/src/test/scala/crystal/PotOptionSpec.scala @@ -102,8 +102,8 @@ class PotOptionSpec extends DisciplineSuite { forAll((t: Throwable) => PotOption.error[Int](t).toOptionTry.contains_(Failure(t))) } - property("PotOption[Int].toOptionTry: ReadySome(a) is Some(Success(a))") { - forAll((i: Int) => PotOption.ReadySome(i).toOptionTry === Success(i).some) + property("PotOption[Int].toOptionTry: ReadySome(a) is Some(Success(Some(a)))") { + forAll((i: Int) => PotOption.ReadySome(i).toOptionTry === Success(i.some).some) } property("PotOption[Int] (Any.ready): a.ready === Ready(a)") {