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

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter5: Repetition Statements
Section5.5: A Closer Look: Loop Programming Techniques
Problem 12E: (Program) Write a program that tests the effectiveness of the rand() library function. Start by...
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
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Fibonacci algorithm
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr