Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. package o1.hofuncs
  2.  
  3. // This program is introduced in Chapter 5.5.
  4.  
  5. import o1._
  6.  
  7. object Task9 extends App {
  8.  
  9. // PHASE 1/2:
  10. // Implement turnElementsIntoResult so that it computes its return value by
  11. // applying a given function (its third parameter) to the numbers contained in
  12. // a given vector (its first parameter). The second parameter indicates the
  13. // base value of the computation (which gets returned if the vector is empty).
  14. //
  15. // For example, say "numbers" contains the integers 20, 10, and 5, "initialValue"
  16. // equals 0 and "operation" is a function that returns the sum of its two parameters.
  17. //
  18. // 1. The "operation" is first applied to "initialValue" and
  19. // the first number in the vector. 0 + 20 equals 20.
  20. // 2. The "operation" is then applied to the previous result
  21. // and the next element. 20 + 10 = 30.
  22. // 3. The operation is again applied to the previous result
  23. // and the next element. 30 + 5 = 35.
  24. // 4. The last element has been processed, so it's time to
  25. // stop and return 35.
  26. //
  27. // As you will have noticed, this example demonstrates one way of summing up
  28. // the elements of a vector.
  29.  
  30. def turnElementsIntoResult(numbers: Vector[Int], initialValue: Int, operation: (Int, Int) => Int) = {
  31. var i = initialValue
  32. if (numbers.isEmpty) { initialValue
  33. } else
  34. for (a <- numbers) {
  35. i = operation(i, a)
  36. }
  37. i
  38. }
  39.  
  40.  
  41. val exampleNumbers = Vector(100, 25, -12, 0, 50, 0)
  42. println("The numbers are: " + exampleNumbers.mkString(", "))
  43.  
  44. def addToSum(existingResult: Int, nextNumber: Int) = existingResult + nextNumber
  45.  
  46. println("Sum: " + turnElementsIntoResult(exampleNumbers, 0, addToSum)) // should print: Sum: 163
  47.  
  48.  
  49. // PHASE 2/2:
  50. // Use turnElementsIntoResult to compute:
  51. // 1) how many positive numbers there are among "exampleNumbers"
  52. // 2) the product of all the other "exampleNumbers" except for zeros
  53. // To that end, flesh out the two helper functions below and replace the zero
  54. // results with calls to turnElementsIntoResult. You'll need to design the
  55. // helper functions yourself so that they work in combination with
  56. // turnElementsIntoResult to produce the desired results.
  57.  
  58. def positiveCount(numbers: Vector[Int]) = {
  59. var counter = 0
  60. for (a <- numbers) {
  61. if (a >= 0)
  62. counter += 1
  63. }
  64. val howManyPositives = counter
  65. }
  66.  
  67. def productOfNonZeros = ??? // TODO: add the appropriate parameters and a method body
  68.  
  69. val howManyPositives = 0 // TODO: pass the appropriate function as a parameter to turnElementsIntoResult; also pick an initial value that works for counting elements
  70. val product = 0 // TODO: pass the appropriate function as a parameter to turnElementsIntoResult; also pick an initial value that works for multiplying elements
  71. println("Number of positive elements: " + howManyPositives) // should print: Number of positive elements: 3
  72. println("Product: " + product) // should print: Product: -1500000
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement