Data Pipeline Patterns
Map Transform
Iterable.map(fn) runs fn on each element and yields a lazy Iterable of the results; .toList() materializes it back into a List. A clean transform is a pure function on one element at a time, so two map calls chain naturally: convert each value, then format each result. The original list is never modified.
Program
Play the program to convert Celsius temperatures to Fahrenheit and then to labels like 32F, then print them joined by spaces.
map_transform.dart
Replay: real traced execution (multi-file project)
void main() {
var celsius = [0, 10, 20];
var fahrenheit = celsius.map((c) => c * 9 ~/ 5 + 32).toList();
var labels = fahrenheit.map((f) => '${f}F').toList();
print(labels.join(' '));
}
celsius ← [0, 10, 20]
1void main() {2 var celsius = [0, 10, 20];3 var fahrenheit = celsius.map((c) => c * 9 ~/ 5 + 32).toList();values this step[0, 10, 20]celsiusfahrenheit ← [32, 50, 68]
2var celsius = [0, 10, 20];3var fahrenheit = celsius.map((c) => c * 9 ~/ 5 + 32).toList();4var labels = fahrenheit.map((f) => '${f}F').toList();values this step[32, 50, 68]fahrenheit[0, 10, 20]celsiuslabels ← [32F, 50F, 68F]
3var fahrenheit = celsius.map((c) => c * 9 ~/ 5 + 32).toList();4var labels = fahrenheit.map((f) => '${f}F').toList();5print(labels.join(' '));values this step[32F, 50F, 68F]labels[32, 50, 68]fahrenheitprint(labels.join(' '));
4 var labels = fahrenheit.map((f) => '${f}F').toList();5 print(labels.join(' '));6}output32F 50F 68Fvalues this step[32F, 50F, 68F]labels
map
`list.map(fn)` returns a lazy `Iterable` of `fn(e)` for each element. The element type can change, e.g. `int -> String`.
toList
`.toList()` materializes a lazy `Iterable` into a fresh `List`, so downstream code can index, iterate again, or print a stable value.
chained transforms
Two `map` calls chain cleanly: each pass is a single, pure value transform. The original `celsius` list is never modified.