How to split a List[Either[A, B]]

前端 未结 8 1155
南笙
南笙 2020-12-10 11:01

I want to split a List[Either[A, B]] in two lists.

Is there a better way ?

def lefts[A, B](eithers : List[Either[A, B]]) : List[A] = eit         


        
8条回答
  •  青春惊慌失措
    2020-12-10 11:20

    You can do it with:

    val (lefts, rights) = eithers.foldRight((List[Int](), List[String]()))((e, p) => e.fold(l => (l :: p._1, p._2), r => (p._1, r :: p._2)))
    

提交回复
热议问题