本文介绍了在scala中将任何方法转换为可重试的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图实现一个应该采用任意方法或代码块的方法,它应该将方法或代码块转换为可重试方法。 以下示例旨在演示我需要的内容 $ p $ import scala.util。{尝试,成功,失败} object Retry { retry [A,B](f:A => Try [B],r:Int):A =>尝试[B] = { //返回一个函数g,并且当用参数 // f调用g时应该尝试(如果失败)r否则返回 //结果首次成功执行f应该从g返回 //。任何使用 //任意数字/类型的参数 code> 解决方案如果你想抽象的arity,这是相当先进的,你必须使用 shapeless ,一个用于泛型编程的库。 建立在@ chengpohi的回答: import shapeless._,ops.function._ import scala.util.Try def retry [ (隐含的fnToP:FnToProduct.Aux [F,L => R],fnFromP:FnFromProduct.Aux [L => R, F):F = { val fn = fnToP(f) def repeat(a:L):R = { for(_ val try = Try(fn(a)) if(tried.isSuccess){ return tried.get } } 抛出新的RuntimeException(s 重试$ r失败)} fnFromP(重复_)} 它的作用: 阶> var i = 0 i:Int = 0 scala> val f = retry((a:Int)=> if(i f:Int => Int = shapeless.ops.FnFromProductInstances$$anon$2$$Lambda$1489/1404497488@1d49a23c scala> f(5)尝试1 尝试2 尝试3 尝试4 尝试5 尝试6 尝试7 尝试8 尝试9 尝试10 res4:Int = 15 scala> var i = 0 i:Int = 0 scala> val f = retry((a:String,b:Int)=> if(i f:(String,Int)=> String = shapeless.ops.FnFromProductInstances$$anon$3$$Lambda$1492/121867201@1a22b89c scala> f(foo,5)尝试1 尝试2 尝试3 尝试4 尝试5 尝试6 尝试7 尝试8 尝试9 尝试10 res5:String = foofoofoofoofoo I am trying to implement a method that should take an arbitrary method or code block and it should convert the method or code-block to retry-able method.Following example is intended to demonstrate what I needimport scala.util.{Try,Success,Failure}object Retry { retry[A, B](f: A => Try[B], r: Int): A => Try[B] = { // return a function g and when g is invoked with parameters // f should be tried (if failed) r number of time otherwise // result of first successful execution of f should be returned // from g. retry should work with any arbitrary function with // any number/type of parameters }} 解决方案 If you want to abstract over arity, which is pretty advanced, you'll have to use shapeless, a library for generic programming.Building on @chengpohi's answer:import shapeless._, ops.function._import scala.util.Trydef retry[F, L <: HList, R](f: F, r: Int = 1)(implicit fnToP: FnToProduct.Aux[F, L => R], fnFromP: FnFromProduct.Aux[L => R, F]): F = { val fn = fnToP(f) def repeat(a: L): R = { for (_ <- 0 to r) { val tried = Try(fn(a)) if (tried.isSuccess) { return tried.get } } throw new RuntimeException(s"retry $r failed") } fnFromP(repeat _)}It works:scala> var i = 0i: Int = 0scala> val f = retry( (a: Int) => if (i < 10) {i += 1; println(s"try $i"); throw new RuntimeException} else a * 3, 42)f: Int => Int = shapeless.ops.FnFromProductInstances$$anon$2$$Lambda$1489/1404497488@1d49a23cscala> f(5)try 1try 2try 3try 4try 5try 6try 7try 8try 9try 10res4: Int = 15scala> var i = 0i: Int = 0scala> val f = retry( (a: String, b: Int) => if (i < 10) {i += 1; println(s"try $i"); throw new RuntimeException} else a * b, 42)f: (String, Int) => String = shapeless.ops.FnFromProductInstances$$anon$3$$Lambda$1492/121867201@1a22b89cscala> f("foo", 5)try 1try 2try 3try 4try 5try 6try 7try 8try 9try 10res5: String = foofoofoofoofoo 这篇关于在scala中将任何方法转换为可重试的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 06:03