Question: Code needs to be in Scala 2D: Loop FoldLeft (10 points) 1 Eliminate the for loop and mutable variables in the code below In [31]:

Code needs to be in Scala

Code needs to be in Scala 2D: Loop FoldLeft (10 points) 1

2D: Loop FoldLeft (10 points) 1 Eliminate the for loop and mutable variables in the code below In [31]: def imperativeFun(ist: List[Int]): Int = { var minSoFar = Int. MaxValue // 2^32-1 var maxDiff = 0 for (x maxDiff) { maxDiff = x - minSoFar return maxDiff Out [31]: defined function imperativeFun In [32]: def funUsingFoldLeft(lst: List[Int]): Int = ??? // YOUR CODE HERE Out [32] : defined function funUsingFoldLeft In [33]: // BEGIN TESTS val 11 = List(1, 2, 3, -1, 2, 5, 18, 19, 1, 0, -12, 2) assert(imperativeFun(11) == funUsingFoldLeft(11), "Test 1 failed") val 12 = List(2) assert(imperativeFun (12) == funUsingFoldLeft(12), "Test 2 failed") val 13 = List() assert(imperativeFun (13) == funUsingFoldLeft(13), "Test 3 failed") val 14 = List(3, 2, 1, 0) assert(imperativeFun (14) == funUsingFoldLeft(14), "Test 4 failed") val 15 = List( -1, -12, -22, -54, -89, 90) assert(imperativeFun (15) == funUsingFoldLeft(15), "Test 5 failed") passed (10) // END TESTS 2D: Loop FoldLeft (10 points) 1 Eliminate the for loop and mutable variables in the code below In [31]: def imperativeFun(ist: List[Int]): Int = { var minSoFar = Int. MaxValue // 2^32-1 var maxDiff = 0 for (x maxDiff) { maxDiff = x - minSoFar return maxDiff Out [31]: defined function imperativeFun In [32]: def funUsingFoldLeft(lst: List[Int]): Int = ??? // YOUR CODE HERE Out [32] : defined function funUsingFoldLeft In [33]: // BEGIN TESTS val 11 = List(1, 2, 3, -1, 2, 5, 18, 19, 1, 0, -12, 2) assert(imperativeFun(11) == funUsingFoldLeft(11), "Test 1 failed") val 12 = List(2) assert(imperativeFun (12) == funUsingFoldLeft(12), "Test 2 failed") val 13 = List() assert(imperativeFun (13) == funUsingFoldLeft(13), "Test 3 failed") val 14 = List(3, 2, 1, 0) assert(imperativeFun (14) == funUsingFoldLeft(14), "Test 4 failed") val 15 = List( -1, -12, -22, -54, -89, 90) assert(imperativeFun (15) == funUsingFoldLeft(15), "Test 5 failed") passed (10) // END TESTS

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!