【问题标题】:Is there maximum number of Validations that can be combined with combinator?是否有最大数量的验证可以与组合器组合?
【发布时间】:2014-08-14 03:37:15
【问题描述】:

我正在尝试使用 Scalaz(scala 2.10,ScalaZ 的 7.1 版)进行验证。我有一个包含 13 个字段的案例类,所以我最终得到了 13 个验证。我正在使用组合器来组合所有验证并在所有验证都成功的情况下构建类。如果只有 12 个组合器,一切都很好。当我添加第 13 个时,我收到一条消息说“值 |@| 不是 scalaz.syntax.ApplicativeBuilder 的成员”。

为了重现,我启动了 REPL 并尝试组合 12 个项目:

scala> (1.some |@| 2.some |@| 3.some |@| 4.some |@| 5.some |@| 6.some |@| 7.some |@|
        8.some |@| 9.some |@| 10.some |@| 11.some |@| 12.some ) 
        {_ + _ + _ + _ + _ + _ + _ + _ + _ +_ +_ +_ }
res11: Option[Int] = Some(78)

工作得很好。然后我尝试组合 13 个项目:

scala> (1.some |@| 2.some |@| 3.some |@| 4.some |@| 5.some |@| 6.some |@| 7.some |@|
        8.some |@| 9.some |@| 10.some |@| 11.some |@| 12.some |@| 13.some) 
        {_ + _ + _ + _ + _ + _ + _ + _ + _ +_ +_ +_ + _}
<console>:14: error: value |@| is not a member of
scalaz.syntax.ApplicativeBuilder[Option,Int,Int]#ApplicativeBuilder3[Int]#
ApplicativeBuilder4[Int]#ApplicativeBuilder5[Int]#ApplicativeBuilder6[Int]#
ApplicativeBuilder7[Int]#ApplicativeBuilder8[Int]#ApplicativeBuilder9[Int]#
ApplicativeBuilder10[Int]#ApplicativeBuilder11[Int]#ApplicativeBuilder12[Int]
(1.some |@| 2.some |@| 3.some |@| 4.some |@| 5.some |@| 6.some |@| 7.some |@| 8.some |@| 9.some |@| 10.some |@| 11.some |@| 12.some |@| 13.some) {_ + _ + _ + _ + _ + _ + _ + _ + _ +_ +_ +_ + _}

还有其他方法可以组合所有验证吗?

【问题讨论】:

  • 通过 Scalaz source code 挖掘,我看到 ApplicativeBuilder 只深入了 12 层 :(.
  • 看我的回答here

标签: scala validation scalaz combinators


【解决方案1】:

为了完整起见:是的,您可以应用应用构建器语法的项目数量是有限的,是的,它是十二个。不过,一般情况下,您可以重写以下内容:

(a |@| b |@| c)(Function.uncurried(foo))

作为:

c <*> (b <*> a.map(foo))

此语法适用于超过 12 个元素。详情请参阅my answer here

【讨论】:

    猜你喜欢
    • 2022-08-02
    • 2021-12-06
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    相关资源
    最近更新 更多