fold, reduce, and expand
Aggregate and flatten iterables.
fold, reduce, and expand is a free Dart Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Aggregating a Collection
Often you want to collapse a whole list into one value, like a sum or a max. Dart gives you fold and reduce for exactly that. 🧮
Meet reduce
reduce combines elements pairwise using your function. It starts with the first item, so the list must not be empty.
var sum = [1, 2, 3, 4].reduce((a, b) => a + b);
print(sum); // 10reduce Carries a Result
Each step receives the running result and the next element. Think of a as the accumulator that grows as you go.
var max = [3, 9, 2].reduce((a, b) => a > b ? a : b);
print(max); // 9The Empty List Trap
Calling reduce on an empty list throws a StateError. When a list might be empty, reach for fold instead.
Meet fold
fold is like reduce but you give it a starting value. That seed sets the result type and handles empty lists safely.
var total = [1, 2, 3].fold(0, (a, b) => a + b);
print(total); // 6fold Can Change Type
Because the seed defines the type, fold can turn a list of ints into a String, a Map, or anything you need.
var joined = [1, 2, 3].fold('', (a, b) => '$a$b');
print(joined); // 123fold on Empty Is Safe
With an empty list, fold simply returns the seed. No crash, no special case to remember.
var s = <int>[].fold(0, (a, b) => a + b);
print(s); // 0reduce vs fold
Use reduce for a quick same-type combine of a non-empty list. Reach for fold when you need a seed or a new result type.
Meet expand
expand maps each element to an iterable, then flattens them all into one stream of values. It is map plus flatten.
var r = [1, 2].expand((n) => [n, n * 10]);
print(r.toList()); // [1, 10, 2, 20]Flatten Nested Lists
A common job for expand is flattening a list of lists into a single flat list in one clean call.
var flat = [[1, 2], [3, 4]].expand((x) => x);
print(flat.toList()); // [1, 2, 3, 4]Chaining the Three
These methods compose beautifully. You can expand to flatten, then fold to aggregate, in a single readable pipeline.
var t = [[1, 2], [3]].expand((x) => x).fold(0, (a, b) => a + b);
print(t); // 6Quick Check
Pick the right tool for an empty-safe sum.
Recap
reduce combines, fold combines with a seed, and expand flattens. Together they aggregate any collection with ease. 🎉
Frequently asked questions
Is the “fold, reduce, and expand” lesson free?
Yes — the full text of “fold, reduce, and expand” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “fold, reduce, and expand”?
Aggregate and flatten iterables. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “fold, reduce, and expand” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.