Skip to content

Commit

Permalink
cogens. better pot conversions.
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiaggio committed Nov 16, 2023
1 parent 3135f15 commit 0efe89c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -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")

Expand Down
2 changes: 2 additions & 0 deletions modules/core/shared/src/main/scala/crystal/Pot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions modules/core/shared/src/main/scala/crystal/PotOption.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
45 changes: 20 additions & 25 deletions modules/testkit/shared/src/main/scala/crystal/arb/arbitraries.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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)") {
Expand Down

0 comments on commit 0efe89c

Please sign in to comment.