简体   繁体   English

使用reduceLeft的应用程序构建器的scalaz

[英]scalaz using reduceLeft for applicative builder

I am little confused about |@| 关于| @ |我有点困惑 magic in scalaz. 斯卡拉兹的魔法。 Here is my code: 这是我的代码:

def isThree(x: Int): Validation[NonEmptyList[String], Int] = if (x!= 3){("failed: %d" format x).wrapNel.failure} else {x.success}

println((isThree(6) |@| isThree(7) |@| isThree(13) ) {_ + _ + _})

output : Failure(NonEmptyList(failed: 6, failed: 7, failed: 13)) This is output is what I want. 输出 :失败(NonEmptyList(失败:6,失败:7,失败:13))这是我想要的输出。

Here is my questions: 这是我的问题:

  1. assume I have sequence of Validation, I want to use applicative builder to chain them together. 假设我有验证序列,我想使用applicative builder将它们链接在一起。

    Seq(isThree(13), isThree(15)).reduceLeft(_ |@| _) why compilation failed due to type not matching ? Seq(isThree(13),isThree(15))。reduceLeft(_ | @ | _)为什么编译因类型不匹配而失败?

  2. It is similar to first question, if I use bracket : 它类似于第一个问题,如果我使用括号

    println((isThree(6) |@| (sThree(7) |@| isThree(13)) ) {_ + _ + _}) , it still has compilation errors. println((isThree(6) |@| (sThree(7) |@| isThree(13)) ) {_ + _ + _}) ,它仍然有编译错误。

Also, I know I can fix the first one by using <* instead of |@|, but I am still confused why is that, it looks not convenient to use. 另外,我知道我可以通过使用<*代替| @来修复第一个,但我仍然感到困惑,为什么这样,它看起来不方便使用。

Many thanks in advance. 提前谢谢了。

This works: |@| 这适用:| @ | result type is not ValidationNel (Applicative) but ApplicativeBuilder, you need yo apply it first to some function 结果类型不是ValidationNel(Applicative)而是ApplicativeBuilder,你需要首先将它应用于某个函数

  import scalaz._, Scalaz._

  val x1: ValidationNel[String, Int] = 1.successNel
  val x2: ValidationNel[String, Int] = 2.successNel
  val x3: ValidationNel[String, Int] = 3.successNel

  println((x1 |@| x2 |@| x3)(_ + _ + _))

  println((x1 :: x2 :: x3 :: Nil).reduceLeft((l, r) => (l |@| r)(_ + _)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM