简体   繁体   English

Scalaz验证忽略申请方法

[英]Scalaz Validation disregard Apply method

    (List("ha", "heh", "hmm") |@| List("?", "!", ".")) {_ +"doeswork"+ _}

returns correctly 正确返回

     res0: List[String] = List(hadoeswork?, hadoeswork!, hadoeswork., hehdoeswork?, hehdoeswork!, hehdoeswork., hmmdoeswork?, hmmdoeswork!, hmmdoeswork.)

while

    (Validation.failure[String, String]("fail") |@| Validation.failure[String, String]("fail")) {_ +"doesnotwork"+ _}

returns invariably 总是返回

    res1: scalaz.Validation[String,String] = Failure(failfail)

whatever function you're passing to "unlift" the values. 您传递的用于“取消”值的任何函数。 Any idea why? 知道为什么吗?

It doesn't do this "invariably". 它不会“总是”执行此操作。 If you tried 如果你尝试过

("fail".success[String] |@| "fail".success[String]) {_ +"doesnotwork"+ _}

it would return Success(faildoesnotworkfail) . 它将返回Success(faildoesnotworkfail) But since not all values passed to |@| 但是由于并非所有值都传递给|@| are Success , it doesn't have anything to apply the {_ + "doesnotwork" + _} to, because this operation is for the success case, not for the failure case. Success ,则没有任何可应用{_ + "doesnotwork" + _}的内容,因为此操作是针对成功案例而不是针对失败案例。

The success case is doing the computations. 成功案例是进行计算。 The failure case only returns the combined error messages. 故障案例仅返回合并的错误消息。 If you try to do computations on Failure , nothing interesting is actually computed, only the summary of errors is returned. 如果您尝试对Failure进行计算,则实际上不会计算任何有趣的事情,仅返回错误摘要。

In your particular case, it just happens to be that it seems as if the function is applicable, because both success values and failure messages have the same type. 在您的特定情况下,恰好似乎该函数适用,因为成功值和失败消息具有相同的类型。 However, in general case, the type of the failure messages doesn't even have to do anything with the type of the function passed instead {_ +"doesnotwork"+ _} . 但是,在一般情况下, failure消息的类型甚至不必对通过传递的函数的类型{_ +"doesnotwork"+ _} For example, your Success values could be Double , and instead of {_ +"doesnotwork"+ _} you could then use {(x, y) => math.atan2(y, x)} , which wouldn't make any sense for String -typed Failure -messages. 例如,您的Success值可以是Double ,然后可以使用{(x, y) => math.atan2(y, x)}代替{_ +"doesnotwork"+ _} {(x, y) => math.atan2(y, x)} ,这不会产生任何作用。 String类型的Failure消息的意义。

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

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