val y: Seq[Either[String, Int]]
case class Response(value: String)
val notFound = Response("Not Found")
val found = Response("Found")
val response: Response = y.lastOption
.map(_.fold(_ => notFound, _ => found))
.getOrElse(notFound)
I am getting a list of Eithers. I want to get the last Either
in the list. I then want to get Right
value. If the list is empty or the Either is Left
, I want to return a default value. The code does this but can this be done more elegantly and clearer in Scala with perhaps cats? I don't use scalaz.