Created
February 5, 2014 10:20
-
-
Save f81/8820694 to your computer and use it in GitHub Desktop.
第18章:Scalaのパターンマッチ ref: http://qiita.com/f81@github/items/aa46c248a38a171ed955
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ scalac PatternMatchConst.scala | |
$ scala PatternMatchConst | |
random:true | |
number:1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ scalac PatternMatchSeq.scala | |
$ scala PatternMatchSeq | |
number:2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ scalac PatternMatchTuple.scala | |
$ scala PatternMatchTuple | |
result:OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ scalac PatternMatchConstructor.scala | |
$ scala PatternMatchConstructor | |
所属:一号生 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ scalac PatternMatchType.scala | |
$ scala PatternMatchType | |
num=1 | |
bool=true | |
name=J |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ scalac PatternMatchWild.scala | |
$ scala PatternMatchWild | |
number:4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ scalac PatternMatchNoWild.scala | |
$ scala PatternMatchNoWild | |
num=1 | |
bool=true | |
scala.MatchError: J (of class java.lang.String) | |
at PatternMatch3_2$$anonfun$main$1.apply(PatternMatch3_2.scala:7) | |
at PatternMatch3_2$$anonfun$main$1.apply(PatternMatch3_2.scala:6) | |
at scala.collection.immutable.List.foreach(List.scala:318) | |
at PatternMatch3_2$.main(PatternMatch3_2.scala:6) | |
at PatternMatch3_2.main(PatternMatch3_2.scala) | |
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:601) | |
at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:71) | |
at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:31) | |
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:139) | |
at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:71) | |
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:139) | |
at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:28) | |
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:45) | |
at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:35) | |
at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:45) | |
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:74) | |
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ scalac PatternMatchVar.scala | |
$ scala PatternMatchVar | |
random:4 | |
number:8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.util.Random | |
object PatternMatchConst { | |
def main(args: Array[String]): Unit = { | |
val random = Random.nextBoolean | |
val num = random match { | |
case true => 1 | |
case false => 0 | |
} | |
println("random:" + random) | |
println("number:" + num) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object PatternMatchConstructor { | |
case class Person(name: String, age: Int, education: String) | |
def main(args: Array[String]): Unit = { | |
val person = Person("椿山 清美", 15, "男塾") | |
val belongTo = person match { | |
case Person("独眼鉄", _, "男塾") => "鎮守直廊三人衆 " | |
case Person("椿山 清美", _, "男塾") => "一号生" | |
case _ => "一般市民" | |
} | |
println("所属:" + belongTo) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object PatternMatchNoWild { | |
def main(args: Array[String]): Unit = { | |
val list = List(1, true, "J") | |
for (value <- list) { | |
value match { | |
case num: Integer => println("num=" + num) | |
case bool: Boolean => println("bool=" + bool) | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object PatternMatchSeq { | |
def main(args: Array[String]): Unit = { | |
val seq = Seq(1, 2, 3, 4) | |
val result = seq match { | |
case Seq(1, a, _*) => a | |
case _ => 0 | |
} | |
println("number:" + result) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object PatternMatchTuple { | |
def main(args: Array[String]): Unit = { | |
val tuple = (1, 2, "OK") | |
val result = tuple match { | |
case (1, 2, x) => x | |
case _ => "NG" | |
} | |
println("result:" + result) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object PatternMatchType { | |
def main(args: Array[String]): Unit = { | |
val list = List(1, true, "J") | |
for (value <- list) { | |
value match { | |
case num: Integer => println("num=" + num) | |
case bool: Boolean => println("bool=" + bool) | |
case name: String => println("name=" + name) | |
case _ => println("value=" + value) | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object PatternMatchVar { | |
def main(args: Array[String]): Unit = { | |
val random = 4 | |
val result = random match { | |
case num => num * 2 | |
} | |
println("random:" + random) | |
println("number:" + result) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object PatternMatchWild { | |
def main(args: Array[String]): Unit = { | |
val num = 4 | |
num match { | |
case _ => println("number:" + num) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment