⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 patterns.scala

📁 JAVA 语言的函数式编程扩展
💻 SCALA
字号:
package examplesobject patterns {  trait Tree  case class Branch(left: Tree, right: Tree) extends Tree  case class Leaf(x: Int) extends Tree  val tree1 = Branch(Branch(Leaf(1), Leaf(2)), Branch(Leaf(3), Leaf(4)))  def sumLeaves(t: Tree): Int = t match {    case Branch(l, r) => sumLeaves(l) + sumLeaves(r)    case Leaf(x) => x  }  def find[a,b](it: Iterator[Pair[a, b]], x: a): Option[b] = {    var result: Option[b] = None    var found = false    while (it.hasNext && !found) {      val Pair(x1, y) = it.next      if (x == x1) { found = true; result = Some(y) }    }    result  }  def printFinds[a](xs: List[Pair[a, String]], x: a) =    find(xs.elements, x) match {      case Some(y) => System.out.println(y)      case None => System.out.println("no match")    }  def main(args: Array[String]) {    println("sum of leafs=" + sumLeaves(tree1))    printFinds(List(Pair(3, "three"), Pair(4, "four")), 4)  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -