Map Filter Reduce
tags: one-dimensional-array map filter reduce
Often you want to traverse one array and perform some operation on each element in the sequence. For example, the following function takes a string as the input and returns a new string that is in upper case.
|
|
An operation like upper_case is called a map because it maps a function (in this case uppercase) onto each of the elements in the sequence.
|
|
An operation like upper_only is called a filter because it selects some of the elements and filters out the others.
Most common array operations can be expressed as a combination of map, filter and reduce.
Reduce operation is a basic building block that traverses a sequence and accumulates the elements into a single result. An example of reduce is [array sum]({% post_url 2018-11-30-array-sum %})
Map operation is a basic building block that traverses a sequence and performs an operation on each element.
Filter operation is a basic building block that traverses a sequence and selects the elements that satisfy some criterion.