lahaarchive.blogg.se

Kotlin any
Kotlin any













kotlin any

Each invocation, one after the other, receives not only the current element, but also the result of the previous calculation, inside the accumulator.

kotlin any

The task of the lambda function is to return a new accumulator. An individual item from our collection.An accumulator, which has the same type as our collection, and.For example, this could be a list of cities in Germany, and we have another list of German license plates that correspond to those cities:Īs seen in the example above, we call the reduce function with a lambda block which receives two parameters: Let's learn about a way to combine two collections, and create a new one from them – it's time to zip!Īssume we have two collections, where the elements at each index are somehow related. So far, we have always looked at a single collection, and what we can do with it. If you are doing an operation on a list, which in turn generates a collection for each one of the input elements, consider if flatMap can help you simplify your code! As desired, we end up with a plain list of elements – the list of characters from the names of the original collection.

kotlin any

Next, that collection of collections gets flattened. In the example above, the function that we provide creates a list for each element in our input collection, containing the letters of the original string. Enter fullscreen mode Exit fullscreen mode















Kotlin any