Partial Function : Example 1

Partial Function use case clause. Rounded brackets cannot be used for Function definition. Only Curly brackets can be used
//#PS:35
scala> val mathNeng: PartialFunction[String, Int] = {
     |   case "math" => 80
     |   case "english" => 75
     | }
mathNeng: PartialFunction[String,Int] = <function1>

scala> val sciNHistory: PartialFunction[String, Int] = {
     |   case "science" => 70
     |   case "history" => 75
     | }
sciNHistory: PartialFunction[String,Int] = <function1>

scala> mathNeng("math")
res23: Int = 80
// Partial function can also be tested
scala> mathNeng.isDefinedAt("biology")
res24: Boolean = false

scala> mathNeng("biology")
scala.MatchError: biology (of class java.lang.String)
 at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:248)
 at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:246)
 at $anonfun$1.applyOrElse(<console>:7)
 at $anonfun$1.applyOrElse(<console>:7)
 at scala.runtime.AbstractPartialFunction$mcIL$sp.apply$mcIL$sp(AbstractPartialFunction.scala:33)
 at scala.runtime.AbstractPartialFunction$mcIL$sp.apply(AbstractPartialFunction.scala:33)
 at scala.runtime.AbstractPartialFunction$mcIL$sp.apply(AbstractPartialFunction.scala:25)
 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:760)
 at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:805)
 at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:717)
 at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:581)
 at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:588)
 at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:591)
 at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:882)
 at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:837)
 at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:837)
 at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
 at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:837)
 at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:83)
 at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
 at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105)
 at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

// Multiple functions can be chained together
scala> val subjects = mathNeng orElse sciNHistory
subjects: PartialFunction[String,Int] = <function1>

scala> subjects("history")
res26: Int = 75

No comments:

Post a Comment