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

前端 未结 8 1156
南笙
南笙 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:26

    A compact, but not CPU-effictive solution:

    val lefts = list.flatMap(_.left.toOption)
    val rights = list.flatMap(_.right.toOption)
    

提交回复
热议问题