
Subscribe to a
paid plan to get instant access to this course.
Video time: 01m41s
Want this course?
Baking with variadic functions
Now we can rework our bake-cake function to use the variadic versions of our add functions.
Exercise 12
Rewrite bake-cake
to use the new add
function.
Cake
2 cups flour
2 eggs
1 cup milk
1 cup sugar
mix all ingredients
bake in pan for 25 minutes
let cool
To navigate to this point in the introduction-to-clojure
repo:
$CMD git checkout -f 1.15
Watch me
Here's the old version:
(defn bake-cake []
(add-eggs 2)
(add-flour-cups 2)
(add-milk-cups 1)
(add-sugar-cups 1)
(mix)
(pour-into-pan)
(bake-pan 25)
(cool-pan))
We can replace each of the calls to add-
functions with add
.
(defn bake-cake []
(add :egg 2)
(add :flour 2)
(add :milk 1)
(add :sugar 1)
(mix)
(pour-into-pan)
(bake-pan 25)
(cool-pan))
It's looking a lot like a recipe now.
Let's test it:
(start-over)
(bake-cake)
To navigate to this point in the introduction-to-clojure
repo:
$CMD git checkout -f 1.16
Code is available: lispcast/introduction-to-clojure
Code for this particular lesson is available at the 1.15
branch.
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 1.15
Introduction to Clojure v2
Introduction
free
12 min
Introducing X5 and JC
free
2 min
Working with the REPL
free
14 min
Defining functions
7 min
Defining a bake-cake function
2 min
Conditionals
10 min
Defining scooped?
2 min
Defining squeezed?
3 min
Defining simple?
1 min
Defining ingredient types
11 min
Looping a number of times
10 min
Using the plural functions
2 min
Variadic functions
15 min
Baking with variadic functions
2 min
Bake cookies function
2 min
Day 1 Conclusion
2 min
Running Leiningen projects
11 min
Error function
11 min
Refactoring errors
4 min
Bakery locations
14 min
Scooped?, squeezed?, and simple? refactor
2 min
Fetching ingredients
9 min
Fetch ingredients generic
3 min
Maps
7 min
Fetching a shopping list
10 min
Refactoring fetch-list
10 min
Refactoring locations
9 min
A day at the bakery
20 min
Analyzing a day at the bakery
3 min
Functional programming
10 min
Into
6 min
multiply-ingredients
7 min
order to ingredients
5 min
orders to ingredients
8 min
Making one shopping trip
3 min
Making one delivery
12 min
Day 2 conclusion
3 min
Day 3
2 min
Add cocoa
2 min
Baking brownies
6 min
Analyzing recipes
6 min
Starting the database
2 min
Perform
1 min
Mix step
1 min
Pouring and baking
1 min
Adding ingredients
7 min
Bake recipe
4 min
Generic bake function
3 min
Adding all recipes
5 min
Cleaning up order to ingredients
6 min
Ingredients in the database
3 min
Refactoring scooped, squeezed, and simple
2 min
Generic fetch ingredient
9 min
Refactoring fetch-list (again)
10 min
Make storage-location more useful
4 min
Redefining add
9 min
Defining actions
4 min
Refactoring perform
4 min
Conclusions
5 min
Appendix: Creating a Leiningen project
10 min