Dart reduce versus fold on collections
WHAT IT TESTS: choosing the right aggregation. OUTLINE: reduce combines same-type elements and throws on empty; fold takes an initial value and accumulator of any type, safe on empty. RED FLAG: using reduce when the result type differs from the elements.
WHAT IT TESTS: whether you understand accumulator typing and empty-collection behavior. ANSWER OUTLINE: reduce(combine) folds elements into a single value of the same element type and throws a StateError on an empty collection because it has no seed. fold(initial, combine) starts from an initial accumulator, which may be a different type, so it handles empty collections safely and can build sums, maps or strings from objects.
Read the original → interview
- #dart
- #collections
- #fold
- #reduce
- #functional
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.