Filtering Values ​​on the Right Side of a Disjunction

I have a result consisting of a list of vectors in a scalaz clause, and I want to be able to examine and filter out elements on 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 an empty element, as well as any values! = 2 from the filled elements, I can do the following:

 for { v <- goodTL f = v.flatten } yield for { i <- f if i != 2 } yield i 

giving scalaz.\/[Nothing,List[Int]] = \/-(List(1, 3, 3, 4)) what I want, but I would like to know if there is a less complicated way to achieve this.

+5
source share
1 answer

Your desugars version of something very similar to the following:

 goodTL.map(_.flatten.filter(_ != 2)) 

This is the case when I personally find the sugar-free version is much clearer about what is happening.

+3
source

Source: https://habr.com/ru/post/1214841/


All Articles