![]() ![]() Recipe 3.18, “Creating Your Own Control Structures”, demonstrates the use of multiple parameter lists. The second parameter list is the block of code you want to run (your algorithm). The first parameter list takes one field, the seed value. If you haven’t seen syntax like that before, foldLeft takes two parameter lists. ![]() In the last two examples, foldLeft uses 20 and then 100 for its first element, which affects the resulting sum as shown. Scala> peeps.reduceLeft((x,y) => if (x.length val a = Array(1, 2, 3) Scala> peeps.reduceLeft((x,y) => if (x.length > y.length) x else y) Vector(al, hannah, emily, christina, aleka) you provide the sequence 'seq' and the function 'f'įor (i val peeps = Vector("al", "hannah", "emily", "christina", "aleka") The code that reduceLeft uses under the hood looks like this: reduceLeft keeps taking the result from the function and comparing it to the next element in the collection, until it marches through all the elements in the collection, ending up with the result, 20. ![]() 15 is larger, so it becomes the new result. 12 is the result of the first comparison, and 15 is the next element in the collection. reduceLeft takes that result (12), and calls findMax(12, 15).findMax returned 12, because 12 is larger than 6. reduceLeft starts by calling findMax to test the first two elements in the array, 12 and 6.The output shows how reduceLeft marches through the elements in the sequence, and how it called the function at each step. Now call reduceLeft again on the array, this time giving it the findMax function: Println(s"compared $x to $y, $winner was larger") The following function does a “max” comparison like the last example, but has some extra debugging code so you can see how reduceLeft works as it marches through the sequence. You can demonstrate how reduceLeft works by creating a larger function. ![]() Res3: Int = 20 Show each step in the process The following examples show how to use reduceLeft to get the product of all elements in the sequence, the smallest value in the sequence, and the largest value: You can write that code like this, if you prefer: The following example shows how to get the sum of all the elements in the sequence:ĭon’t let the underscores throw you for a loop they just stand for the two parameters that are passed into your function. Given that sequence, use reduceLeft to determine different properties about the collection. First, create a sample collection to experiment with: The best way to show this is with some examples. If you’ve never used these methods before, you’ll see that they give you a surprising amount of power. That result is compared to the fourth element to yield a new result, and so on. That result is compared with the third element, and that comparison yields a new result. reduceLeft starts by comparing the first two elements in the collection with your algorithm, and returns a result. Related methods, such as scanLeft and scanRight, are also shown in the Discussion.įor example, use reduceLeft to walk through a sequence from left to right (from the first element to the last). Use the reduceLeft, foldLeft, reduceRight, and foldRight methods to walk through the elements in a sequence, applying your function to neighboring elements to yield a new result, which is then compared to the next element in the sequence to yield a new result. You want to walk through all of the elements in a Scala sequence, comparing two neighboring elements as you walk through the collection. This is Recipe 10.20, “How to Walk Through a Scala Collection with the reduce and fold Methods” Problem This is an excerpt from the 1st Edition of the Scala Cookbook (partially modified for the internet). show more info on classes/objects in repl. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |