Dart reduce versus fold on collections
choosing the right aggregation.
reduce combines same-type elements and throws on empty; fold takes an initial value and accumulator of any type, safe on empty.
using reduce when the result type differs from the elements.
What's really being asked
Whether you know how each combiner types its accumulator and how each behaves on an empty collection, then can pick correctly.
The full answer
reduce takes a binary combine function and starts with the first element, folding the rest into it, so the result is always the same type as the elements. With an empty iterable it throws a StateError because there is no first element to seed from. fold takes an explicit initial value plus a combine function whose accumulator can be any type independent of the element type. Because it has a seed, fold returns that seed for an empty collection instead of throwing, and it can transform elements into a different result type.
The mistakes people make
Claiming they are interchangeable. Using reduce to sum a numeric property of a list of objects, which is awkward because the accumulator must stay the element type, not an int. Forgetting that reduce crashes on empty input. Thinking fold cannot change the result type.
What usually comes next
What exception does reduce throw on empty. Can fold build a Map. How does generic type inference work for the seed. When is reduce more readable.
A concrete example
Given a List of Order objects each with a numeric total, you cannot cleanly write orders.reduce because the accumulator would have to be an Order, not a number. Instead you write orders.fold(0, (sum, order) => sum + order.total). Here the seed 0 is an int, the accumulator type differs from the element type Order, and if orders is empty the call safely returns 0 rather than throwing. reduce fits only when the result is the same type as the elements, such as numbers.reduce((a, b) => a + b) on a non-empty list of ints.
Interview question
Why is fold, not reduce, the right choice for summing a numeric property across a list of objects?
- a.fold is the only method that iterates the whole collection
- b.fold's accumulator can be a different type (an int) from the elements, and it is safe on empty listsCorrect
- c.reduce automatically multiplies instead of adding
- d.reduce cannot accept a combine function at all
Why? this is the answer
fold supplies a typed seed so the accumulator (an int sum) need not match the element type and returns the seed on empty input. reduce forces the result to the element type and throws StateError on an empty collection.
Just read this? Test yourself on what you have been reading.
- #dart
- #collections
- #fold
- #reduce
- #functional
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on dart — each one lists the topics its interview covers.
See open roles