Skip to content

Instantly share code, notes, and snippets.

@yuroyoro
Created May 27, 2011 17:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save yuroyoro/995712 to your computer and use it in GitHub Desktop.
retry機能着きLoan pattern
scala> def retry[T](p: => Boolean)(f: => T):T =
| try{ f } catch { case e => if(p) retry(p)(f) else throw e }
retry: [T](p: => Boolean)(f: => T)T
scala> retry( util.Random.nextInt(3) == 0 ){ println("trying..."); "a".toInt }
trying...
java.lang.NumberFormatException: For input string: "a"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at scala.collection.immutable.StringLike$class.toInt(StringLike.scala:231)
at scala.collection.immutable.StringOps.toInt(StringOps.scala:31)
at $anonfun$2.apply$mcI$sp(<console>:13)
at $anonfun$2.apply(<console>:13)
at $anonfun$2.apply(<console>:13)
at .retry(<console>:13)
at .<init>(<console>:13)
at .<clinit>(<console>)
at .<init>(<console>:11)
at .<clinit>(<console>)
at $export(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:592)
at scala.tools.nsc.interpreter.IMain$Request$$anonfun$10.apply(IMain.scala:828)
at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
at scala.tools.nsc.io.package$$anon$2.run(package.scala:31)
at java.lang.Thread.run(Thread.java:680)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment