Porting the Garden Season Report
You’ve ported Rosa’s script one part at a time. In this project, most of it goes into one file for the booking page.
A port that runs can still offer plot 3 to the wrong person. So each dict, set and counter is written the JavaScript way, and the port should print Python’s first ten lines. The seed swap lines are left for the exercise.
- The section and show class tuples become frozen arrays, with
Object.freeze(). A plot’s fields are names you write, so it stays an object, and its two crop sets,cropsandlastYear, turn into Sets. - The waiting list keeps member numbers in the order people asked, so it becomes a Map. An object would sort the numbers and offer the plot to June Harris, member 42. The new applicants and the work days follow: a Map and two Sets.
- JavaScript has no set comprehension, so the port adds each holder’s name to an empty Set in a
for...ofloop, until array methods come later. - Each set operator becomes a method that returns a new Set:
union(),intersection()anddifference(). The pipe,|, turns Sets into numbers, so the turnout would readundefined of 9..sizestands in forlen(), and each result is spread into an array and sorted before it’s joined. - Python compares each plot’s crops with last year’s using
==. JavaScript’s===asks whether two Sets are the same object, so no plot would match. The port calls a helper,sameMembers(), which checks the size, then whether one Set is a subset of the other. The loop pushes each matching plot’s number. - Plot 4’s crops went in as potatoes, then kale, and last year’s as kale, then potatoes.
sameMembers()still counts plot 4 as the same: a Set keeps the order its members went in, butisSubsetOf()only asks about members, as Python’s==does. - Python merges the applicants in with
|=. The port loops over their Map and sets each pair, and the square brackets in the loop header destructure it, like tuple unpacking. - The first key,
[...waiting.keys()][0], replacesnext(iter(waiting)), andpop()becomesget(), thendelete(). With brackets, Dev Patel would stay listed and Ola Bakare never join. Then a loop finds plot 3 and gives it to Dev Patel, with an empty Set of crops. - Python’s
defaultdict(list)becomesMap.groupBy(), which groups the whole plots array in one call. The append loop, ported line by line, would stop on the first plot. - Section D has nobody yet, so its group is missing, and the lookup falls back to an empty array
with
?? []. D prints zero plots, as in Python. - Python’s
Counterbecomes a Map of counts, each read as the stored number or zero. One loop over the show classes builds the counts line, and a second picks the classes under three. - Unlike a
Counter, a Map has no zero for a missing class. Read without the default, runner beans would printundefinedand never be canceled.
Here’s the port, garden-report.js, with the seed swap still to come: