Want this course?
Analyzing a day at the bakery
Let's take a look at the
day-at-the-bakery function and see how we can improve it.
Name some things that can be improved in this function. They can include ways to make it more efficient (saving battery life), less annoying to the delivery bot, or better code. Name one way to fix each problem.
To navigate to this point in the introduction-to-clojure repo:
$CMD git checkout -f 2.14
Well, here are four things I found that I think we should address.
X5 will make many trips to the pantry and fridge.
- X5 should make one trip before baking.
X5 will deliver a receipt for every item.
- X5 should deliver one receipt per order.
We repeat the code for cakes and cookies.
- We should factor out the common code.
We are calling
fetch-liston an inline map.
- We should extract the map and name it.
The first two points are about thinking ahead and "batching" the work. We'll have to make one big list of ingredients so we can go fetch them in one trip. And we need to bake all the cakes and cookies, gathering up the receipts, and deliver them all at once. Let's take a look at how we can approach this problem.
Code is available: lispcast/introduction-to-clojure
Code for this particular lesson is available at the
You can checkout the code in your local repo with this command:
$CMD git clone https://github.com/lispcast/introduction-to-clojure.git $CMD cd introduction-to-clojure $CMD git checkout -f 2.14