Chapter 14 — Functional Programming on Collections
Part 1A — Learning Dart · Chapter 14 of 62 · 6 h
Chapter 14 — Functional Programming on Collections
Level: intermediate Estimated time: 6 h Prerequisites: chapter 05 (loops), chapter 06 (collections), chapter 07 (functions), chapter 13 (exceptions) What you will be able to do by the end: transform, filter, sort, and summarize any list of players, enemies, or inventory items in a few declarative lines, without writing a single
forloop.
14.0 — Chapter Objectives
By the end of this chapter, you will be able to:
- distinguish the imperative style (how to do it) from the declarative style (what to get);
- write an anonymous function and an arrow function to pass to a method;
- iterate over a collection with
forEach(); - transform a collection with
map(), including changing its type; - explain what an
Iterableis and whytoList()is often required; - filter a collection with
where(); - search for an element with
firstWhere(),lastWhere(),singleWhere(), andindexWhere(); - answer a yes-or-no question with
any(),every(), andcontains(); - reduce a collection to a single value with
reduce()andfold(); - choose between
reduce()andfold()with full knowledge of the trade-offs; - sort a list with
sort()and write a comparator; - use
compareTo()on numbers and on strings; - sort objects by score, by name, and then by several criteria;
- sort a copy without modifying the original list;
- slice a collection with
take()andskip(); - flatten nested lists with
expand(); - remove duplicates with
toSet(); - assemble a collection into a string with
join(); - use the spread operator
...and its null-aware version...?; - write an
ifand afordirectly inside a list literal; - chain several operations into a single readable expression;
- decide, using clear criteria, when it is better to go back to a
forloop.
Comments
Comments are powered by GitHub Discussions
Sign in with GitHub to join the discussion