DMarlon Studio

Lesson 17 · 17 of 20

forEach, map, where, reduce and fold · Dart

Go through and transform Dart collections with forEach, map, where, reduce and fold, and chain them to filter, convert and add up in one go.

Dart · forEach, map, where, reduce and fold in DMarlon Studio: the example's code and its output in the terminal

These methods take an anonymous function (lesson 13) and apply it to each element. `forEach` simply runs it with each one: names.forEach((name) => print('Welcome, $name')) greets all three.

`map` turns each element into something else: each order becomes a label like 24.00 EUR. `where` keeps only the elements that meet the condition, here orders of 20 euros or more. Both give back a sequence, which `toList()` turns into a list.

`reduce` combines the elements two at a time until only one is left: with (a, b) => a + b it keeps adding and gives the total, 155.75, and with (a, b) => a > b ? a : b it keeps the larger one at each step, 61.0.

`fold` is like reduce, but it starts from an initial value, which can even be of another type: fold(0, ...) counts the small orders starting from 0, and fold('', ...) builds a string with the initials of a list of names.

Since each method returns a collection, they can be chained: the last example keeps the orders over 10 euros with where, turns each one into points with map and ~/, and adds them up with fold. It reads from top to bottom, like a recipe.

Code · main.dart

void main() {  var names = ['Ana', 'Ben', 'Carla'];  var orders = [24.0, 8.5, 61.0, 15.25, 42.0, 5.0];  // forEach runs a function with each element.  names.forEach((name) => print('Welcome, $name'));  // map turns each element into another; toList makes a list.  var labels = orders.map((o) => '${o.toStringAsFixed(2)} EUR').toList();  print('Labels: $labels');  // where keeps only the elements that meet the condition.  var big = orders.where((o) => o >= 20).toList();  print('Orders of 20 or more: $big');  // reduce combines the elements two by two into one.  var total = orders.reduce((a, b) => a + b);  var highest = orders.reduce((a, b) => a > b ? a : b);  print('Total: $total, highest: $highest');  // fold is like reduce, but starts from a value of any type.  var small = orders.fold(0, (count, o) => o < 10 ? count + 1 : count);  var initials = names.fold('', (text, name) => text + name[0]);  print('Small orders: $small, initials: $initials');  // They chain: filter, transform and add up in one go.  var points = orders      .where((o) => o > 10)      .map((o) => o ~/ 10)      .fold(0, (sum, p) => sum + p);  print('Loyalty points: $points');}

Output · dart run main.dart

Welcome, Ana
Welcome, Ben
Welcome, Carla
Labels: [24.00 EUR, 8.50 EUR, 61.00 EUR, 15.25 EUR, 42.00 EUR, 5.00 EUR]
Orders of 20 or more: [24.0, 61.0, 42.0]
Total: 155.75, highest: 61.0
Small orders: 2, initials: ABC
Loyalty points: 13

Key points

  • forEach runs a function with each element.
  • map transforms; where filters.
  • toList() turns the result into a list.
  • reduce combines everything into one value; fold too, from a starting value.
  • The methods chain: where(...).map(...).fold(...).

Exercise

With a list of ages, use where to keep the adults, map to turn them into 'X years' labels, and fold to add up all the ages.