addzuloo.blogg.se

Scala foldleft
Scala foldleft













So now we see our collection and z standing on the left side of the collection.Īs discussed earlier, we understood that the first argument is of type B. And the left represents the direction from which we start folding. The fold in the foldLeft indicates what we want to do. Now let’s say we have a collection of some numbers and we want to calculate the sum of these numbers. You may be thinking why is it called foldLeft? So in essence, foldLeft takes an initial element to start with and a function with two arguments then applies the binary operator iteratively and gets the final result. The first argument should be of type B and the second argument should be of type A. One key thing to note here is that the order of the arguments will matter in this function. This function takes two arguments, applies the binary operator, and computes the result. Next, the function takes a function op here. There is no relationship defined between A and B in the declaration. Here B may be the same type as A, related to A, or unrelated to A. It takes two different arguments in two different function calls.įor a collection of type A, it returns a result of type B. The first call argument as mentioned z represents the initial value of type B that should be used. Now let’s break it down and try to understand what the declaration means. In order to understand, let’s look at what the function declaration looks like. So, now let’s get started.įoldLeft performs iteration on the collection elements. the fold method internally invokes the foldLeft method. In this blog, we will look into them in detail and try to understand how they work.īefore moving ahead, I want to clarify that the fold method is just a wrapper to foldLeft, i.e. Two methods named scanLeftand scanRightwalk through a sequence in a manner similar to reduceLeftand reduceRight, but they return a sequence instead of a single value.įor instance, scanLeft“Produces a collection containing cumulative results of applying the operator going left to right.The fold method is a Higher Order Function in Scala and it has two variant namely, The following examples demonstrate a “sum” algorithm, first with reduceLeftand then with foldLeft, to demonstrate the difference: scala The foldLeftmethod works just like reduceLeft, but it lets you set a seed value to be used for the first element.

SCALA FOLDLEFT HOW TO

The following examples show how to use reduceLeftto get the product of all elements in the sequence, the smallest value in the sequence, and the largest value: > The following example shows how to get the sum of all the elements in the sequence: scala

scala foldleft

Given that sequence, use reduceLeftto determine different properties about the collection. 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.

scala foldleft

reduceLeftstarts by comparing the first two elements in the collection with your algorithm, and returns a result. Related methods, such as scanLeftand scanRight, are also shown in the Discussion.įor example, use reduceLeftto walk through a sequence from left to right (from the first element to the last). Use the reduceLeft, foldLeft, reduceRight, and foldRightmethods 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.

scala foldleft

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 Scala Cookbook(partially modified for the internet).













Scala foldleft