Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to flatten disjunction type

Tags:

scala

scalaz

If I have a following method

 def getMyList :\/[Throwable,List[\/[Throwable,Int]]] ={
 ....
 }

how to flatten type of getMyList to \/[Throwable,List[Int]]

like image 281
Vikas Pandya Avatar asked Sep 22 '14 23:09

Vikas Pandya


2 Answers

Just flatMap and sequenceU, it's all in scalaz:

  def flatten(e: \/[Throwable,List[\/[Throwable,Int]]]): \/[Throwable,List[Int]] = {
    e.flatMap(a => a.sequenceU)
  }
like image 99
Noah Avatar answered Sep 21 '22 06:09

Noah


If by flatten, you mean remove the left types from List[\/[Throwable,Int]], then you can map the outer disjunction, and collect the right types:

list.map(_.collect{ case \/-(x) => x})
like image 24
Michael Zajac Avatar answered Sep 21 '22 06:09

Michael Zajac