返回顶部

收藏

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 14:00:31字符串之模式匹配 by 杨德伟
  3. 2015-05-31 14:09:06泛型编程示例:Eventable by 二糊
  4. 2015-07-04 11:04:16Scala 之 Socket 通讯示例 by 杨洋
  5. 2015-04-04 18:55:57简单swing程序 by 梁方
  6. 2015-06-02 11:04:53Scala 进行参数匹配 by jack.chen
  7. 2015-07-09 21:21:23Scala Lazy Evaluation by Hugh
  8. 2015-04-07 09:24:55求1~n的全排列 by 阿明娃儿
  9. 2015-06-07 09:39:00scala 函数编程 by jxzhoujian
  10. 2015-07-10 20:53:17Scala 之 Properties by fengsweat
  11. 2018-03-30 21:56:50java自动识别用户上传的文本文件编码 by Hugh
相关聚客文章
  1. importnewzz 发表 2015-10-16 10:30:44 Scala的模式匹配
  2. 四火 发表 2015-09-20 17:02:00 Scala的模式匹配
  3. noreply@blogger.com 发表 2008-11-21 17:53:00 Scala?
  4. 方 腾飞 发表 2013-08-09 13:04:52 讨喜的隔离可变性(八)类型化角色和Murmurs
  5. 锦轩 发表 2014-03-17 20:22:21 Java编程者一分钟上手Scala
  6. Freewind 发表 2014-05-09 15:15:22 问候sbt一千遍之:添加全局镜像库
  7. 博主 发表 2014-01-26 08:00:00 一个有意思的Scala函数式编程例子
  8. 博主 发表 2014-06-14 06:46:00 Desugar Scala(15) -- unapply和unapplySeq方法
  9. Freewind 发表 2011-09-12 17:56:00 scala一周研究成果
  10. Freewind 发表 2011-09-13 14:30:56 关键字type
  11. Freewind 发表 2013-01-01 09:00:16 05. Http正文解析器
  12. hongjiang 发表 2014-10-04 14:23:34 scala雾中风景(14): trait的泛型参数为何不支持context bounds

发表评论