Linear Scan
tags: linear-scan one-loop one-dimensional-array map filter reduce forward-iteration reverse-iteration array-traversal
A sequence is an ordered collection of values where each value is identified by an integer index.
An array with sequence of elements can be scanned left to right. This is forward iteration:
|
|
Every item in the array is examined by using a for loop with index to access the elements in the array. This is array traversal.
The reverse iteration scans the array from right to left:
|
|
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.
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.