In this lesson we'll introduce a new programming paradigm that is particular useful for working with data: map-reduce-filter.
We'll manipulated linear collections of data using good old
But we can do better.
Let's see how!
Many of the code and algorithms we've written together have operated on sequential data, stored in either arrays or
And we've seen and identified common patterns for working with this kind of data.
Such as counting:
And while these are fine building blocks for creating larger programs, there is something a bit repetitive and dull about them.
Every one has the same overall structure: the same
loop, many have an
Shouldn't there be a better, more compact way of expressing these kind of patterns?
In Kotlin we refer to this as
map-reduce-filter, which is also sometimes known
as stream data processing.
map-reduce-filter allows us to work with sequential data by composing powerful programming primitives to great effect.
These methods are built right in to all of the Kotlin collections—arrays, lists, and maps—that we've already been working with!
Let's examine how to utilize common stream operations to replace the repetitive loop-based code we wrote above.
First, let's look at one of the most basic collection operations—
We can also filter streams using...
Let's see how:
And, we can even reduce a
Stream until a single value with
reduce, a surprisingly powerful primitive.
This programming pattern may seem alien to you at first. That's not surprising. A famous silicon valley tech thought leader has pointed out that powerful programming ideas usually feel strange and even bizarre at first. But, as you come to appreciate them, not only do they become more natural, but the older less-powerful ways of doing things start to see even more limited.
map-reduce-filter pipelines are:
for-loop based patterns we've listed above have been factored out, leaving only the decision-making logic that changes depending on the application
map, for example) to be done in parallel, increasing the speed with which large collections can be processed
To wrap up, let's have some fun working with one of our favorite data sets:
Need more practice? Head over to the practice page.