## 2016年7月12日 星期二

### [Scala 小學堂] Scala Gossic : 繼續深入 - 模式比對 (密封類別)

Source From Here

1. case class Point(x: Int, y: Int)
2. case class Circle(p: Point, r: Int)
3. case class Cylinder(c: Circle, h: Int)

1. def what(a: Any) = a match {
2.     case Point(_, _)    => "點"
3.     case Circle(_, _)   => "圓"
4.     case Cylinder(_, _) => "柱"
5. }

1. sealed abstract class Drawing
2. case class Point(x: Int, y: Int) extends Drawing
3. case class Circle(p: Point, r: Int) extends Drawing
4. case class Cylinder(c: Circle, h: Int) extends Drawing

1. sealed trait Drawing
2. case class Point(x: Int, y: Int) extends Drawing
3. case class Circle(p: Point, r: Int) extends Drawing
4. case class Cylinder(c: Circle, h: Int) extends Drawing

1. def what(d: Drawing) = d match {
2.     case Point(_, _)    => "點"
3.     case Cylinder(_, _) => "柱"
4. }

1. def what(d: Drawing) = d match {
2.     case Point(_, _)    => "點"
3.     case Cylinder(_, _) => "柱"
4.     case _              => "" // 作你想作的事，或者丟出例外
5. }

1. sealed trait Drawing
2. case class Point(x: Int, y: Int) extends Drawing
3. case class Circle(p: Point, r: Int) extends Drawing
4. case class Cylinder(c: Circle, h: Int) extends Drawing
5.
6. def what(d: Drawing) = (d: @unchecked) match {
7.     case Point(_, _)    => "點"
8.     case Cylinder(_, _) => "柱"
9. }
10.
11. val p = Point(12)
12. val c = Circle(p, 5)
13. val cy = Cylinder(c, 10)
14. println(what(p))        // 點
15. // println(what(c))     // Will throws scala.MatchError
16. println(what(cy))       // 柱

### [ Python 文章收集 ] List Comprehensions and Generator Expressions

Source From  Here   Preface   Do you know the difference between the following syntax?  view plain copy to clipboard print ? [x  for ...