5/16/2023 0 Comments Scala foldleftLet us assume that we have an object of type on which we would like to perform a set of transformations. So, closing this way too long introduction, let us see the true power behind foldLeft(). This is equivalent to input.foldLeft(0)(op) but intended for people who love Perl. (0 :: Nil).reduce(op) //empty list is prepended by 0Įven worse, many suggest “simplified" foldLeft() syntax, I doubt it simplifies anything: After all we can simply prepend initial value to the input list and happily use reduce(): It is sad how many tutorial stop right here. If the collection is empty, foldLeft() returns the initial value. The subsequent iterations remain the same. In this case the op() function is first called with initial value 0 as the first argument and with the first collection element: Here is where foldLeft() enters with the ability to specify initial value: Let's face it, typically we implement factorial for the first (and last) time somewhere at the beginning of the university and to add up numbers we have a convenience method:īesides the problem with empty collections is a bit painful - after all the sum of empty set of numbers is intuitively equal to 0 and the concatenation of an empty set of strings is. When it is empty, reduce() will throw an exception. It is worth to mention two special cases: when the collection has only one element, reduce() returns this very element. Obviously we are not limited to addition operator:ĭef factorial(x: Int) = (2 to x).reduce(_ * _) Note the shorthand notation for code block without naming the parameters: _ + _. Example use-cases include adding up numbers, concatenating a sequence of strings, etc.: So far so good - reduce() reduces a collection of a given type to a single value of the same type. When we realize that op() is basically an addition: The result ( 8) is passed as a first argument to a subsequent invocation of op where the second argument is the next collection element:įrom the logical standpoint the following composed operation has been invoked: reduce() will invoke that operation on two first items of the collection: Both parameters of that function (and its return value) need to have the same type as the collection. The code is more-or-less readable: to reduce method we are passing 2-argument function op (operation). Input.reduce((total, cur) => total + cur)ĭef op(total: Int, cur: Int) = total + cur I always believed that a working code is worth a thousand words: Doesn't sound very encouraging but as we will see soon there are great some use-cases waiting to be discovered.īefore we dive into foldLeft, let us have a look at reduce - simplified version of foldLeft. Second argument is always the current item in the collection. You may want to pimp out the String class.Ap| 8 Minute Read foldLeft() method, available for all collections in Scala, allows to run a given 2-argument function against consecutive elements of that collection, where the result of that function is passed as the first argument in the next invocation. This is really messy to write, so you won't want to do this twice. If you decide to build two counters and compare them, you'll need to do the same fold logic twice. It's better to just build a second counter. You'll need to remove keys from the counter if any of the values go to 0, which lowers the signal to noise ratio of the code. I find the subtracting of the counts to be very confusing. Compare the counts from each string directly! You can use Map#getOrElse and provide a default - in this case, it makes sense to provide a 0 default since you haven't seen the character. It's good that you know m(c) can throw if there's a missing key luckily, In the case that they aren't the same, you don't need to incur a linear pass for an equality check.
0 Comments
Leave a Reply. |