Boss lesson · Unit 03
Trees of things
Treat one thing and a group of things the same way
What you practise
- Composite and iterator: files and folders, or shapes and groups, treated the same way
- Walking the structure without knowing its layout
Try a question from this lesson
True or false?
Code that asks an item for its size must first check whether the item is a File or a Folder.
Correct answer
False
Why
Both answer the same method, so the caller just asks. Removing that check from every caller is the entire reason for the pattern.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Composite
Give the single thing and the group the same method. A file answers size() from its own number. A folder answers it by asking each child and adding up.
A child can be a file or another folder: the folder does not check. Callers do not check either.
class File:
def __init__(self, n):
self.n = n
def size(self):
return self.n
class Folder:
def __init__(self, kids):
self.kids = kids
def size(self):
t = 0
for k in self.kids:
t += k.size()
return tclass File {
constructor(n) { this.n = n; }
size() { return this.n; }
}
class Folder {
constructor(kids) { this.kids = kids; }
size() {
let t = 0;
for (const k of this.kids) {
t += k.size();
}
return t;
}
}interface Item { size(): number }
class File implements Item {
constructor(private n: number) {}
size() { return this.n; }
}
class Folder implements Item {
constructor(private kids: Item[]) {}
size() {
let t = 0;
for (const k of this.kids) {
t += k.size();
}
return t;
}
}TypeScript noteimplements Item asks the compiler to check that the class really has size(): number. A folder holds Item[], so it accepts files and folders alike.
Concept
Iterator
An iterator hands out the items of a structure one at a time, so the caller can write a plain loop without knowing how the structure is nested.
For a tree, a generator does it in a few lines: yield a file, or walk into each child and pass on whatever that walk yields.
def walk(node):
if isinstance(node, File):
yield node
else:
for k in node.kids:
yield from walk(k)function* walk(it) {
if (it instanceof File) {
yield it;
return;
}
for (const k of it.kids) yield* walk(k);
}type Item = File | Folder;
type Files = Generator<File>;
function* walk(it: Item): Files {
if (it instanceof File) {
yield it;
return;
}
for (const k of it.kids) yield* walk(k);
}Python noteyield from passes on every value that the inner generator produces, as if this function had yielded them itself.
JavaScript notefunction* makes a generator. yield* passes on everything that another generator yields.
TypeScript noteGenerator<File> is the type of a generator that yields File objects. After the instanceof check, it is known to be a Folder.
9 more exercises in the app
- 4× type the answer
- 1× fill the blanks
- 2× multiple choice
- 1× order the lines
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.