I have a result consisting of a list of Vectors in a scalaz disjunction and I want to be able to examine and filter out elements from within the right side.
simplified example:
import scalaz._
import Scalaz._
type TL = Throwable \/ List[Vector[Int]]
val goodTL: TL = \/-(List(Vector(1,2,3),Vector(), Vector(2,3,4)))
If I want to remove empty element and also any values != 2 from populated elements I can do the following:
for {
v <- goodTL
f = v.flatten
} yield for {
i <- f
if i != 2
} yield i
giving a scalaz.\/[Nothing,List[Int]] = \/-(List(1, 3, 3, 4))
which is what I want but I would like to know if there is a less convoluted way of achieving this.
Your version desugars to something very similar to the following:
goodTL.map(_.flatten.filter(_ != 2))
This is a case where I personally find the sugar-free version a lot clearer about what's going on.
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