【问题标题】:Using Refined for Retry?使用 Refined 重试?
【发布时间】:2017-08-29 19:05:28
【问题描述】:

使用精炼,我试图定义f

import eu.timepit.refined._
import eu.timepit.refined.api.Refined
import eu.timepit.refined.auto._
import eu.timepit.refined.numeric._

// if action 'succeeds', return 'good'; otherwise re-try, subtracting count by 1
scala> def f[A](action: => A, success: A => Boolean, count: Int Refined Positive): String = 
     |   if( success(action) ) "good" else f(action, success, count - 1)
<console>:32: error: compile-time refinement only works with literals
         if( success(action) ) "good" else f(action, success, count - 1)
                                                                    ^

由于没有用,我求助于:

def fEither[A](action: => A, success: A => Boolean, count: Either[String, Int Refined Positive]): String = { 
  println(count)

  if( success(action) ) "good" 
  else {
    count match {
      case Right(c) => fEither(action, success, refineV[Positive](c - 1))
      case Left(_)  => "bad"
    }
  }
}

scala> fEither[Int](42, _ => false, Right( refineMV[Positive]( 2 ) ) )
Right(2)
Right(1)
Left(Predicate failed: (0 > 0).)
res2: String = bad

理想情况下,我想将此 Idris 程序转换为 Scala:

f : (action : a) -> (success: a -> Bool) -> (n : Nat) -> String
f action success (S n) = if (success action) then "good" else f action success n
f _       _      Z     = "bad"

*scratch> f 42 (const False) 2
"bad" : String
*scratch> f 42 (const False) 0
"bad" : String

但我不确定Nat 功能是否存在任何模式匹配。

【问题讨论】:

    标签: scala refined


    【解决方案1】:
    • 您要使用的细化是NonNegative,因此0 是一个有效值。
    • Idris 代码主要区分 n - 1 是否仍然是自然数,因此您可以直接使用运行时版本 refineV

    def f[A](action: => A, success: A => Boolean, count: Int Refined NonNegative): String =
      refineV[NonNegative](count - 1) match {
        case Right(n) => if (success(action)) "good" else f(action, success, n)
        case Left(_)  => "bad"
      }
    

    附:您可能需要多个参数列表 b/c Scala 可能无法在调用站点正确推断 A 的类型

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      • 2022-07-14
      • 1970-01-01
      • 2021-10-05
      • 2019-01-20
      • 2021-09-25
      相关资源
      最近更新 更多