Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering a set of Either[x, y], with logging

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.

like image 784
TheDude Avatar asked Dec 01 '22 09:12

TheDude


1 Answers

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 }
like image 132
Mario Galic Avatar answered Dec 05 '22 05:12

Mario Galic