Skip to content

Instantly share code, notes, and snippets.

@gakuzzzz
Created February 21, 2014 12:41
Show Gist options
  • Save gakuzzzz/9133588 to your computer and use it in GitHub Desktop.
Save gakuzzzz/9133588 to your computer and use it in GitHub Desktop.
PartialFunction
scala> val list: Seq[PartialFunction[Int, String]] = Seq({case 1 => "a"}, {case 2 => "b"}, {case 3 => "c"})
list: Seq[PartialFunction[Int,String]] = List(<function1>, <function1>, <function1>)
scala> list.reduceLeft(_ orElse _).apply(100)
scala.MatchError: 100 (of class java.lang.Integer)
at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:248)
at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:246)
at $anonfun$3.applyOrElse(<console>:7)
at $anonfun$3.applyOrElse(<console>:7)
at scala.runtime.AbstractPartialFunction$mcLI$sp.apply$mcLI$sp(AbstractPartialFunction.scala:33)
at scala.runtime.AbstractPartialFunction$mcLI$sp.apply(AbstractPartialFunction.scala:33)
at scala.runtime.AbstractPartialFunction$mcLI$sp.apply(AbstractPartialFunction.scala:25)
at $anonfun$2.applyOrElse(<console>:7)
at $anonfun$2.applyOrElse(<console>:7)
at scala.PartialFunction$OrElse.apply(PartialFunction.scala:162)
at $anonfun$1.applyOrElse(<console>:7)
at $anonfun$1.applyOrElse(<console>:7)
at scala.PartialFunction$OrElse.apply(PartialFunction.scala:162)
at .<init>(<console>:9)
at .<clinit>(<console>)
at .<init>(<console>:7)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734)
at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:745)
at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:790)
at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:702)
at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:566)
at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:573)
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:576)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:867)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:889)
at xsbt.ConsoleInterface.run(ConsoleInterface.scala:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:102)
at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:77)
at sbt.Console.sbt$Console$$console0$1(Console.scala:23)
at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)
at sbt.TrapExit$.sbt$TrapExit$$executeMain$1(TrapExit.scala:33)
at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)
scala>
@nazoking
Copy link

多分 apply でもチェックするようにしないと最後のものがapplyされてしまう?

val list: Seq[PartialFunction[Int, String]] = Seq(new PartialFunction[Int,String]{ def isDefinedAt(a:Int)=a==1; def apply(a:Int)="a"},new PartialFunction[Int,String]{ def isDefinedAt(a:Int)=a==2; def apply(a:Int)="b"})

list: Seq[PartialFunction[Int,String]] = List(<function1>, <function1>)


 list.reduceLeft(_ orElse _).apply(2)

res4: String = b


 list.reduceLeft(_ orElse _).apply(1)

res5: String = a


 list.reduceLeft(_ orElse _).apply(0)

res6: String = b


list.reduceLeft(_ orElse _).lift(0)

res7: Option[String] = None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment