Maybe you've heard of Clojure. Maybe you even tried to learn it. If you did, you may have had some of these problems:
These problems are hard to figure out on your own, and many tutorials skip over these. Wouldn't it be nice if you had:
Let me tell you about ...
Introduction to Clojure is a gentle tutorial for Clojure. It will get you started quickly and painlessly. You'll teach a robot how to bake cookies, cake, and brownies--and learn Clojure along the way!
This is the updated and expanded course that has been used by hundreds of people to learn Clojure over the years.
If you don't learn as much from this course as you thought, just ask for a refund within 30 days and I'll give you your money back.
Guide to baking for the intrepid robot
How to use the course. What is Clojure and why do people use it? How to get set up for the course.
We introduce X5, who wants to be a baker. He's going to baking school. JC is his baking instructor. We'll be helping X5 learn, by using Clojure.
We start off our journey by baking some cookies and a cake. We learn how to use each ingredient and how to follow recipes.
We write a function that can tell us whether we need to scoop an ingredient.
Let's learn how to iterate a certain number of times using dotimes.
We learn to write functions that can take a variable number of arguments and use a common pattern for handling default argument values.
Now we can rework our bake-cake function to use the variadic versions of our add functions.
We can now define a function called bake-cookies to implement the cookies recipe we thought before.
Let's wrap up our first day at the bakery and take a look at what we've accomplished.
In this first lesson, we'll take a tour of a Clojure project and learn how to run it. We'll also take a look at the editor we'll be using to edit the code for the rest of the course.
Let's now use the error function we just wrote and replace all of the error expressions we've already got with calls to it.
We learn about the different locations that X5 can go to in the bakery. We learn about sets and what they can hold.
We refactor scooped?, squeezed?, and simple? to use the same pattern we just wrote.
We need to go fetch ingredients from different locations. Let's write a function that will go to the right location and fetch an ingredient and bring it back to the prep area.
We can now write a function that will just take the ingredient as an argument and decide where to go to fetch it.
We learn a new conditional and how we can begin fetching an entire shopping list with as few trips as possible.
We have some duplication. Let's learn a new loop to go through all of the locations and fetch what we need from each.
Now that we have an efficient way of fetching ingredients, let's handle the orders for the day.
Let's take a look at the
day-at-the-bakery function and see how we can improve it.
Let's talk about Functional Programming. Then we can talk about combining ingredient lists.
We also want to be able to multiply an ingredient list. For instance, if we need to make 3 cakes, we can multiply the ingredients for a cake by 3. That's not quite as easy as adding, but it's not hard. We just need a few more things.
Now we can learn how to calculate one list from another and how to use it to multiply ingredients.
We create a function that will take an order and return the ingredients needed to complete it.
Now we can write a function that takes a list of orders and gives you the whole list of ingredients you'll need to complete all of them.
The delivery bot is annoyed that we give him one receipt per baked good. Let's fix that by grouping them by order.
Today is X5's third day at the bakery. The first day, we learned how to use ingredients. In the second day, we learned how to calculate, basically plan ahead, before we started working, to make things more efficient.
Let's start with cleaning up some unused code.
We make a generic
bake-recipe function that looks up the steps in the database and bakes it.
Now that we can bake recipes, let's add the other recipes, cookies and brownies.
We get rid of the ingredients listed in the definition of order->ingredients and instead look them up in the database.
We need to add a section to the database about where to fetch ingredients and how to use them.
simple? to use the baking database.
We can now do a generic
fetch-ingredient function that looks up the location in the database. That lets us get rid of the specific
Currently, we've defined a map called
locations that lists the locations of all of the ingredients. It's duplicating what's in the database.
We learn a new tool, called
group-by, that will allow us to drive the fetching of an entire list of ingredients by that list alone.
I'm not completely happy with
storage-location. It's not a very valuable function outside of this one usage. We should really use a custom function just for the item-amount usage.