返回顶部

收藏

Scala 模式匹配 (Case Classes)

更多

使用Case Classes进行模式匹配

patterns.scala

package examples

/** Illustrate the use of pattern matching in Scala. */
object patterns {

  /** We need an abstract base class for trees. Subclasses with 
   *  the 'case' modifier can be used in pattern matching expressions 
   *  to deconstruct trees. 
   */
  abstract class Tree
  case class Branch(left: Tree, right: Tree) extends Tree
  case class Leaf(x: Int) extends Tree

  /** Case classes have an implicit constructor methods which allows 
   *  to create objects withouth the 'new' keyword. It saves some typing 
   *  and makes code clearer. 
   */
  val tree1 = Branch(Branch(Leaf(1), Leaf(2)), Branch(Leaf(3), Leaf(4)))

  /** Return the sum of numbers found in leaves. 
   *  'match' is a generalization of 'switch' in C-like languages 
   * 
   *  Patterns consist of case class constructors (which can 
   *  be nested), and lower case variables which are 
   *  bound to the values with which the class has been constructed. 
   */
  def sumLeaves(t: Tree): Int = t match {
    case Branch(l, r) => sumLeaves(l) + sumLeaves(r)
    case Leaf(x) => x
  }

  /** This illustrates the use of Option types. Since the 
   *  method is not known in advance to find 'x', the 
   *  return type is an Option. Options have two possible 
   *  values, either 'Some' or 'None'. It is a type-safe 
   *  way around 'null' values. 
   */
  def find[A, B](it: Iterator[(A, B)], x: A): Option[B] = {
    var result: Option[B] = None
    while (it.hasNext && result == None) {
      val Pair(x1, y) = it.next
      if (x == x1) result = Some(y)
    }
    result
  }

  def printFinds[A](xs: List[(A, String)], x: A) =
    find(xs.elements, x) match {
      case Some(y) => println(y)
      case None => println("no match")
    }

  def main(args: Array[String]) {
    println("sum of leafs=" + sumLeaves(tree1))
    printFinds(List((3, "three"), (4, "four")), 4)
  }
}

标签:模式匹配,Scala

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2015-06-17 20:13:43Scala 模式匹配(Extractor) by zetaliang
  2. 2015-04-04 18:55:57简单swing程序 by 梁方
  3. 2015-06-02 11:04:53Scala 进行参数匹配 by jack.chen
  4. 2015-07-09 21:21:23Scala Lazy Evaluation by Hugh
  5. 2015-04-07 09:24:55求1~n的全排列 by 阿明娃儿
  6. 2015-06-07 09:39:00scala 函数编程 by jxzhoujian
  7. 2015-07-10 20:53:17Scala 之 Properties by fengsweat
  8. 2015-04-08 17:23:57对一个函数连续执行N次的简洁写法 by 傅小黑
  9. 2015-06-08 19:35:44地址簿 addressbook.scala by tsl0922
  10. 2015-08-03 19:46:27Scala Actors by 跳跳虎
  11. 2018-12-02 10:32:05Mac OS 下使用80端口 by 甄码农
相关聚客文章
  1. 四火 发表 2015-09-20 17:02:00 Scala的模式匹配
  2. importnewzz 发表 2015-10-16 10:30:44 Scala的模式匹配
  3. 博主 发表 2011-08-16 07:00:00 使用maven进行scala项目的构建
  4. 方 腾飞 发表 2013-08-09 13:05:38 讨喜的隔离可变性(九)混合使用角色和STM
  5. 博主 发表 2013-08-02 14:07:00 Spray的模块结构
  6. 博主 发表 2013-05-05 14:02:00 Scala中的语言特性是如何实现的(1)
  7. Freewind 发表 2014-05-28 13:41:47 使用brew安装scala 2.8.x
  8. 博主 发表 2014-06-21 03:32:00 Desugar Scala(16) -- lower bound
  9. Freewind 发表 2011-09-12 17:56:38 scala,离天堂只差一步
  10. Freewind 发表 2011-09-13 14:34:35 关于flatMap的用法
  11. Freewind 发表 2013-01-01 09:00:59 06. Action 的组合
  12. hongjiang 发表 2014-10-05 08:13:21 scala雾中风景(15): class A { type T }与class A[T] {}

发表评论