Skip to content

Commit

Permalink
add filter and filterNot to Pot
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiaggio committed Dec 11, 2023
1 parent fe78a2d commit e0907bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
8 changes: 8 additions & 0 deletions modules/core/shared/src/main/scala/crystal/Pot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ sealed trait Pot[+A] {
}

def toOptionEither: Option[Either[Throwable, A]] = toOptionTry.map(_.toEither)

def filter(f: A => Boolean): Pot[A] =
this match {
case Pot.Ready(a) if !f(a) => Pot.pending
case _ => this
}

def filterNot(f: A => Boolean): Pot[A] = filter(a => !f(a))
}

object Pot {
Expand Down
16 changes: 10 additions & 6 deletions modules/tests/shared/src/test/scala/crystal/PotSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,15 @@ class PotSpec extends DisciplineSuite {
}
}

// property("Ready(None).flatOpt === Pending") {
// Pot.Ready(None).flatOpt == Pot.Pending
// }
property("Pot[Int].filter") {
forAll { (p: Pot[Int]) =>
p.filter(_ > 0) === p.flatMap(i => if (i > 0) Pot.Ready(i) else Pot.Pending)
}
}

// property("Pot[Option[Int]] (Pot.flatten): Ready(Some(a)).flatOpt === Ready(a)") {
// forAll((i: Int) => Pot.Ready(Some(i)).flatOpt === Pot.Ready(i))
// }
property("Pot[Int].filterNot") {
forAll { (p: Pot[Int]) =>
p.filterNot(_ > 0) === p.flatMap(i => if (i <= 0) Pot.Ready(i) else Pot.Pending)
}
}
}

0 comments on commit e0907bf

Please sign in to comment.