The discussion about this problem involved a lot of important topics for the Scala programmer: tail recursion, folding, partial functions, partially applied functions, currying, anonymous functions, type inference and placeholder syntax. This simply takes advantage of the function length() developed in this post and of the function findKth() from problem 03. In the given solution for the problem, the anonymous function can be written more explicitly likeĭef lastNth ( n : Int, l : List ): A = The Scala tour has a good explanation of anonymous functions, which are pretty simple. There is another thing to note in the folding solution, however, which is the way Scala defines anonymous functions. ![]() I consider that two answers to be exhaustive so I will not add any explanation about this topic. I recommend two readings from Stackoverflow: this answer about the meaning of partial function, partially applied function and currying, and this answer (which also links the first one) that explores the difference between partially applied and currying. This has to do with the concepts of partially applied functions (NOT partial functions) and currying. The first strange thing that I see here is that the function is defined with two sets of brackets, the first encompassing "the start value" z and the second what seems the definition of a function. The first element receives the initial value. So basically the foldLeft() method visits each element of the list from left to right and applies a function passing the result of the previous application. If you are unsure on how to use option in Scala, feel free to visit our tutorial on using option.Def foldLeft ( z : B )( f : ( B, A ) ⇒ B ): BĪnd the documentation states: "Applies a binary operator to a start value and all elements of this sequence, going left to right".Using reduceLeftoption will NOT throw any exception = None Step 10: How to use reduceLeftOption to avoid exception if the collection is empty Println("\nStep 4: How to find the cheapest donut using reduceLeft function") We can also use the reduceLeft method to find the minimum element in the Sequence of donut prices as shown below. Sum of elements from donutPrices by calling reduceLeft function explicitly= 6.0Ĥ. How to find the cheapest donut using reduceLeft function Step 3: How to find the sum of elements using reduceLeft function explicitly Println(s"Sum of elements from donutPrices by calling reduceLeft function explicitly= $sum1") Val sum1: Double = donutPrices.reduceLeft((a, b) => a b) Println("\nStep 3: How to find the sum of elements using reduceLeft function explicitly") The example below is similar to the one in Step 2 except that we are using the reduce method explicitly to capture the operator function. In Step 2, we used the reduceLeft method along with the wildcard operator. Step 2: How to find the sum of the elements using reduceLeft functionģ. How to find the sum of elements using reduceLeft function explicitly Println(s"Sum of elements from donutPrices = $sum") Val sum: Double = donutPrices.reduceLeft(_ _) Println("\nStep 2: How to find the sum of the elements using reduceLeft function") The code below shows how to use the reduceLeft method to find the sum of the Sequence of type Double from Step 1, that is, the sum of donut prices. Step 1: How to initialize a sequence of donut pricesĮlements of donutPrices = List(1.5, 2.0, 2.5)Ģ. How to find the sum of the elements using reduceLeft function You should see the following output when you run your Scala application in IntelliJ: ![]() Println(s"Elements of donutPrices = $donutPrices") println("Step 1: How to initialize a sequence of donut prices") ![]() The code below show how to initialize a Sequence of type Double to represent donut prices. How to initialize a sequence of donut prices
0 Comments
Leave a Reply. |