0%
Functional Programming on Collections — Chapter 14

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 for loop.


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 Iterable is and why toList() is often required;
  • filter a collection with where();
  • search for an element with firstWhere(), lastWhere(), singleWhere(), and indexWhere();
  • answer a yes-or-no question with any(), every(), and contains();
  • reduce a collection to a single value with reduce() and fold();
  • choose between reduce() and fold() 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() and skip();
  • 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 if and a for directly 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 for loop.

Comments

Comments are powered by GitHub Discussions

Sign in with GitHub to join the discussion

Link copied!