Iterables
Learning objectives
- You practice the basics of programming with Dart.
- You know the basics of working with iterables using functional programming with Dart.
Iterables
Iterables are objects that can return their contents one object at a time. In Dart, this functionality is implemented through the Iterable-class, which provides the functionality for iterating over -- or going over the contents of -- a collection.
Both List and Set implement the Iterable, which makes it easy to iterate over the values in the collections. Iterating over values in a Map is also possible, although due to maps containing key-value -pairs, iteration is marginally different.
Calling a function with forEach
Iterable collections have a method forEach, which takes a function as a parameter. When the method forEach
is called, the function given to it as a parameter is called once for every item in the collection.
The following example outlines a program with a function called printItem
and a main function where all the items in a collection are iterated over using forEach
with the printItem
function.
The same functionality could be achieved by introducing an anonymous function that is called for every item in the list. The following program behaves similarly to the above program.
Similar functionality is available for sets and maps. For maps, though, as we are working with key-value -pairs, each entry is a key-value -pair, which are iterated over as follows.
Filtering with where
When iterating over a collection, we can filter the iterated values using the where
method. The method returns an iterable, which allows using its methods to decide what to do with the remaining values. In the following example, the where
method is used to limit the iterable to values that are larger than 1, which are then subsequently printed. Below, we use the print
function directly, as it works the same way as the previous printItem
.
Transforming with map
Another useful method when handling iterables is map, which allows transforming values from one to another. In the following example, we multiply numbers in an iterable by two and then print them.
Accessing with elementAt
Iterables are not lists and do not have the syntactic sugar []
for accessing elements. If we wish to access an element at a certain index, we use the method elementAt
. The following example shows how the second odd item is found from a list.
Collecting with toList and toSet
Iterables can be transformed into a list using the toList
method and to sets using the toSet
method. The following example demonstrates the use of the both.
Note that while the previous example shows how to transform an iterable to a data collection, you can also assign an iterable to a variable as shown in the following example.
Folding an iterable
Iterables can be reduced to a single value using the fold
function. The fold
function iteratively combines values of the iterable, leading to a single value. The function takes two parameters: a starting value and a two-parameter function describing how values in the iterable should be combined.
The following function demonstrates merging a list containing strings to a single string variable.
Similarly, iterables containing numbers could be processed. The following example demonstrates calculating the sum of numbers in a list.