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

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 scala
package 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 Breaks

private 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
        }
      }
    }
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值