How to flatten a sequence of cats' ValidatedNel values

后端 未结 1 1224
栀梦
栀梦 2021-02-07 06:05

I need to flatten a sequence of cats.data.ValidatedNel[E, T] values to a single ValidatedNel value:

val results: Seq[cats.data.Validate         


        
相关标签:
1条回答
  • 2021-02-07 07:08

    It depends on how you want to combine them (what is validatedValue in your question ?)

    import cats.data.{Validated, ValidatedNel}
    import cats.implicits._
    
    val validations1 = List(1.validNel[String], 2.valid, 3.valid)
    val validations2 = List(1.validNel[String], "kaboom".invalidNel, "boom".invalidNel)
    

    If you want to combine the Ts, you can use Foldable.combineAll which uses a Monoid[T] :

    val valSum1 = validations1.combineAll 
    // Valid(6)
    val valSum2 = validations2.combineAll 
    // Invalid(OneAnd(kaboom,List(boom)))
    

    If you want to get a ValidationNel[String, List[T]], you can use Traverse.sequence :

    val valList1: ValidatedNel[String, List[Int]] = validations1.sequence
    // Valid(List(1, 2, 3))
    val valList2: ValidatedNel[String, List[Int]] = validations2.sequence
    // Invalid(OneAnd(kaboom,List(boom)))
    

    If you don't care about the result, which seems to be the case, you can use Foldable.sequence_.

    val result1: ValidatedNel[String, Unit] = validations1.sequence_
    //  Valid(())
    val result2: ValidatedNel[String, Unit] = validations2.sequence_
    // Invalid(OneAnd(kaboom,List(boom)))
    
    validations1.sequence_.as(validatedValue) // as(x) is equal to map(_ => x)
    
    0 讨论(0)
提交回复
热议问题