Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question
100%

Your tasks

  • Implement the functions marked with ??? in withProb.scala. (as shown in below)
package withProb:

  /**
   * NOTE: The seed should be System.nanoTime or something similar
   * in order to get real pseudo-randomness but we fix it now
   * for the sake of repeatability.
   */
  val rand = new scala.util.Random(2105)

  /**
   * Executes the given code with probability "prob".
   * For instance,
   *   doWithProb(30.0) { Console.println("I'm feeling lucky!") }
   * shall print the text to the console with probability of 30 percent.
   *
   * Note that you have to modify the type of the "thenAction" parameter so that it admits code
   * to be passed in "call-by-name" evaluation mode.
   * Please see the course material and
   * http://docs.scala-lang.org/tour/automatic-closures.html
   */
  def doWithProb(prob: Double)(thenAction: Unit): Unit =
    require(0.0 <= prob && prob <= 100.0)
    ???
  end doWithProb

  /**
   * The expression
   *   withProb(prob) {choice1} otherwise {choice2}
   * returns the value of the first choice code with probability prob and
   * the value of the second choice with probability 1-prob.
   *
   * For instance,
   *  val x = 2
   *  val y = 3
   *  val z: Int = withProb(30.0) {x*y} otherwise {0}
   * shall return 6 with 30% probability and 0 with 70% probability.
   *
   * Observe that we can also write a statement like
   *  withProb(70.0) { println("first action") } otherwise { println("second action") }
   * The return value is just ignored.
   *
   * Note that you have to modify the types of the choice1 and choice2 parameters so that they admit code
   * with correct return type to be passed in "call-by-name" evaluation mode.
   * Again, please see the course material and especially
   * http://docs.scala-lang.org/tour/automatic-closures.html
   * for help.
   */
  def withProb[T](prob: Double)(choice1: Any): withProbElse[T] =
    require(0.0 <= prob && prob <= 100.0)
    ???
  end withProb

  protected class withProbElse[T](prob: Double, choice1: Any):
    def otherwise(choice2:  Any): T =
      ???
    end otherwise

  end withProbElse
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education