Given a Set[Either[BadObject, GoodObject]], I'd like to convert it into a Set[GoodObject], while logging all the BadObjects.
The problem I am having is that when I try to add logging in a collect call, like:
someMethodThatReurnsTheSet.collect {
case Right(value) => value
case Left(res) => logger.warn(s"Bad object : $res")
}
This changes the return value and I am getting a Set[None], which is not what I want.
Try partition in combination with chaining
import util.chaining._
someMethodThatReurnsTheSet
.partition(_.isRight)
.tap { case (_, lefts) => logger.warn(s"Bad objects $lefts") }
.pipe { case (rights, _) => rights }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With