【scala 笔记(4)】 控制结构 -- 自定义控制结构

来源:互联网 发布:足迹美图软件 编辑:程序博客网 时间:2024/06/05 14:50

scala 内置控制结构仅有 if 、 while、 for、 try 、match 几种,因为它提供了可以创建自己的控制抽象的能力;我们先了解下 函数柯里化(Currying)

自定义控制结构 - using

使用过 Python 的同学应该都知道 上下文管理器, 这有利于资源的释放, 下面也将使用 Scala 实现个类似的文件读写上下文管理器控制结构

object Test {  def main(args: Array[String]): Unit = {    /**      * Scala 泛型编程 [A <: {def close(): Unit}, B] 表示: A对象下面必须有close 方法      * 使用 using 控制结构进行资源释放操作, 使用于读写文件、数据库等操作。      * */    def using[A <: {def close(): Unit}, B](fp: A)(func: A => B): B =      try { func(fp) } finally { fp.close() }    using(new FileWriter("./data.txt")){      fp => {        for(i <- 1 to 3)          fp.write("abc %d\r\n".format(i))      }    }  }}

scala中的Breaks

scala 的 break 不是内置的, 是自定义的,使用throw Exception来实现代码块跳转操作, 还是尽量避免使用吧, 来看一下它实现的源码。

/*                     __                                               *\**     ________ ___   / /  ___     Scala API                            ****    / __/ __// _ | / /  / _ |    (c) 2003-2013, LAMP/EPFL             ****  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **** /____/\___/_/ |_/____/_/ | |                                         ****                          |/                                          **\*                                                                      */package scalapackage util.control/** A class that can be instantiated for the break control abstraction. *  Example usage: *  {{{ *  val mybreaks = new Breaks *  import mybreaks.{break, breakable} * *  breakable { *    for (...) { *      if (...) break() *    } *  } *  }}} *  Calls to break from one instantiation of `Breaks` will never *  target breakable objects of some other instantiation. */class Breaks {  private val breakException = new BreakControl  /**   * A block from which one can exit with a `break`. The `break` may be   * executed further down in the call stack provided that it is called on the   * exact same instance of `Breaks`.   */  def breakable(op: => Unit) {    try {      op    } catch {      case ex: BreakControl =>        if (ex ne breakException) throw ex    }  }  sealed trait TryBlock[T] {    def catchBreak(onBreak: =>T): T  }  /**   * This variant enables the execution of a code block in case of a `break()`:   * {{{   * tryBreakable {   *   for (...) {   *     if (...) break()   *   }   * } catchBreak {   *   doCleanup()   * }   * }}}   */  def tryBreakable[T](op: =>T) = new TryBlock[T] {    def catchBreak(onBreak: =>T) = try {      op    } catch {      case ex: BreakControl =>        if (ex ne breakException) throw ex        onBreak    }  }  /**   * Break from dynamically closest enclosing breakable block using this exact   * `Breaks` instance.   *   * @note This might be different than the statically closest enclosing block!   */  def break(): Nothing = { throw breakException }}/** An object that can be used for the break control abstraction. *  Example usage: *  {{{ *  import Breaks.{break, breakable} * *  breakable { *    for (...) { *      if (...) break *    } *  } *  }}} */object Breaks extends Breaksprivate class BreakControl extends ControlThrowable

利用 scala Breaks 模拟break和continue

import util.control.Breaks._object Test {  def main(args: Array[String]): Unit = {    // break  样例    breakable {      for (i <- 1 to 10) {        println(i)        if (i > 4) break  // break out of the for loop      }    }    // continue 样例    val searchMe = "peter piper picked a peck of pickled peppers"    var numPs = 0    for (i <- 0 until searchMe.length) {      breakable {        if (searchMe.charAt(i) != 'p') {          break  // break out of the 'breakable', continue the outside loop        } else {          numPs += 1        }      }    }  }}
原创粉丝点击