Scalactic Accumulation 的 withGood of Seq

Scalactic Accumulation's withGood of Seq

如何在 Seq[Or] 上使用 withGood 来累积 Scalactic 的 Or?

所以我有类似下面的代码:

val cs: Seq[BigDecimal Or ErrorMessage]

所以我需要在 cs 中的所有值都像这样 Good 时做一些事情

Accumulation.withGood(cs){...}

感谢任何帮助

这是一种方法:

    object worksheet {
      import org.scalactic._

      val test1: Seq[Or[Long, Every[ErrorMessage]]] = Seq(
          Good(1L),
          Good(2L),
          Bad(One("An error")),
          Good(4L),
          Bad(One("Another error"))
          )                                           //> test1  : Seq[org.scalactic.Or[Long,org.scalactic.Every[org.scalactic.ErrorMe
                                                      //| ssage]]] = List(Good(1), Good(2), Bad(One(An error)), Good(4), Bad(One(Anoth
                                                      //| er error)))
      val test2: Seq[Or[Long, Every[ErrorMessage]]] = Seq(
          Good(1L),
          Good(2L),
          Good(3L),
          Good(4L),
          Good(5L)
          )                                           //> test2  : Seq[org.scalactic.Or[Long,org.scalactic.Every[org.scalactic.ErrorMe
                                                      //| ssage]]] = List(Good(1), Good(2), Good(3), Good(4), Good(5))

      test1.foldLeft(Good(Seq()): Or[Seq[Long], Every[ErrorMessage]]) { (x, y) =>
        Accumulation.withGood(x, y) { (x, y) =>
          x :+ y
        }
      }                                               //> res0: org.scalactic.Or[Seq[Long],org.scalactic.Every[org.scalactic.ErrorMess
                                                      //| age]] = Bad(Many(An error, Another error))

      test2.foldLeft(Good(Seq()): Or[Seq[Long], Every[ErrorMessage]]) { (x, y) =>
        Accumulation.withGood(x, y) { (x, y) =>
          x :+ y
        }
      }                                               //> res1: org.scalactic.Or[Seq[Long],org.scalactic.Every[org.scalactic.ErrorMess
                                                      //| age]] = Good(List(1, 2, 3, 4, 5))

    }

或者,根据 http://www.scalactic.org/user_guide/OrAndEvery:

object worksheet {
  println("Welcome to the Scala worksheet")   //> Welcome to the Scala worksheet

  import org.scalactic._
  import Accumulation._

  val test1: Seq[Or[Long, Every[ErrorMessage]]] = Seq(
      Good(1L),
      Good(2L),
      Bad(One("An error")),
      Good(4L),
      Bad(One("Another error"))
      )                                       //> test1  : Seq[org.scalactic.Or[Long,org.scalactic.Every[org.scalactic.ErrorMe
                                              //| ssage]]] = List(Good(1), Good(2), Bad(One(An error)), Good(4), Bad(One(Anoth
                                              //| er error)))
  val test2: Seq[Or[Long, Every[ErrorMessage]]] = Seq(
      Good(1L),
      Good(2L),
      Good(3L),
      Good(4L),
      Good(5L)
      )                                       //> test2  : Seq[org.scalactic.Or[Long,org.scalactic.Every[org.scalactic.ErrorMe
                                              //| ssage]]] = List(Good(1), Good(2), Good(3), Good(4), Good(5))

  test1.combined                              //> res0: org.scalactic.Or[Seq[Long],org.scalactic.Every[org.scalactic.ErrorMess
                                              //| age]] = Bad(Many(An error, Another error))

  test2.combined                              //> res1: org.scalactic.Or[Seq[Long],org.scalactic.Every[org.scalactic.ErrorMess
                                              //| age]] = Good(List(1, 2, 3, 4, 5))
}

是的,你做对了。

累积上的withGood方法,将好的值应用于给定函数,这里是您将在{...}中提供的函数,return结果包裹在Good else it returns 包含所有错误的 Bad,即。一个 Bad,其 Every 包括出现在任何 Bad 中的每个值。

这里是 withGood() 的定义:

 def withGood[A, B, ERR, RESULT](
        a: A Or Every[ERR],
        b: B Or Every[ERR]
      )(
        fn: (A, B) => RESULT
      ): RESULT Or Every[ERR] = withGoodCurried(a, b)(fn.curried)