Iterator
The iterator data type in Filtrera is used to represent sequences of values that can be iterated over. This reference guide provides a detailed specification of the iterator data type, its syntax, and usage for users already familiar with the concept.
Iterators in Filtrera are sequences of values that can be processed one at a time. Iterators are often provided by the runtime, such as database connectors, but there is also a built-in iterator that can be defined as a constant using square brackets, known as a List.
Type Notation for Iterators
The type notation for iterators is [<type>]. This indicates that the iterator contains elements of the specified type.
Example
let numbers: [number] = [1, 2, 3, 4, 5]In this example, numbers is a built-in iterator containing values of type number.
Defining Iterators
Built-in Iterator (List)
The built-in iterator can be defined using square brackets. This is referred to as a List in terms of implementation but functions as an iterator.
Example
let numbers = [1, 2, 3, 4, 5]In this example, numbers is a built-in iterator containing the values 1 to 5.
Indexer Access
Technically, a List is actually a Map with a “1-based” Number index key.
Example
let numbers = [1, 2, 3, 4, 5]from numbers->1 // Returns 1from numbers->(numbers count) // Returns 5Runtime-provided Iterators
Other iterators can be provided by the runtime, such as database connectors or other data sources. These iterators are not defined in the Filtrera language itself but are made available through the runtime environment.
Accessing Iterator Values
Since iterators are sequences of values, they do not support index-based access. Instead, filters and functions are used to process and access the values within iterators.
Example: Using the first Filter
The first filter can be used to access the first item in an iterator.
let numbers = [1, 2, 3, 4, 5]let firstNumber = numbers first
from firstNumberIn this example, firstNumber is 1, the first item in the numbers iterator.
Practical Usage
Example: Filtering an Iterator
let numbers = [1, 2, 3, 4, 5, 6]let isEven = (x) => x mod 2 == 0let evenNumbers = numbers where (x => isEven(x))
from evenNumbersIn this example, evenNumbers is an iterator containing the even numbers from the numbers iterator.
Example: Mapping an Iterator
let numbers = [1, 2, 3, 4, 5]let double = (x) => x * 2let doubledNumbers = numbers select (x => double(x))
from doubledNumbersIn this example, doubledNumbers is an iterator containing the doubled values from the numbers iterator.
Example: Skipping Items in an Iterator
let numbers = [1, 2, 3, 4, 5]let skippedNumbers = numbers skip 2
from skippedNumbersIn this example, skippedNumbers is an iterator with the first two items skipped, containing the values 3, 4, and 5.
Example: Counting Items in an Iterator
let numbers = [1, 2, 3, 4, 5]let countNumbers = numbers count
from countNumbersIn this example, countNumbers is the count of items in the numbers iterator, which is 5.
Summary
The iterator data type in Filtrera represents sequences of values that can be processed one at a time. Built-in iterators, referred to as Lists, can be defined using square brackets, while other iterators can be provided by the runtime. By understanding its syntax and usage, including defining iterators, accessing values, and using filters and functions such as first, select, skip, and count, you can leverage iterators to create expressive and functional programs. The ability to handle runtime-provided iterators further enhances the flexibility and robustness of your Filtrera programs.