Want to learn Clojure? It doesn't have to be hard!
Maybe you've heard of Clojure. Maybe you even tried to learn it. If you did, you may have had some of these problems:
- Trouble getting Clojure installed and working
- What text editor should you use?
- The syntax is foreign and strange
- There's so much to learn
- What practical thing can you even build?
These problems are hard to figure out on your own, and many tutorials skip over these. Wouldn't it be nice if you had:
- A clear guide to installing Clojure on your system
- A great text editor without installing anything new
- A gentle introduction to the syntax without dwelling on it
- Someone teach you just what you need to learn right when you need it
- A fun, engaging activity to get you programming right away
Let me tell you about ...
Introduction to Clojure (v2)
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.
Introduction to Clojure v2
Want this course?
Love it or leave it guarantee
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.
File downloads for this course are below. Jump down
Introduction to Clojure v2
How to use the course. What is Clojure and why do people use it? How to get set up for the course.
Introducing X5 and JC
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.
Working with the REPL
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.
Looping a number of times
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.
Baking with variadic functions
Now we can rework our bake-cake function to use the variadic versions of our add functions.
Bake cookies function
We can now define a function called bake-cookies to implement the cookies recipe we thought before.
Day 1 Conclusion
Let's wrap up our first day at the bakery and take a look at what we've accomplished.
Running Leiningen projects
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.
Scooped?, squeezed?, and simple? refactor
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.
Fetch ingredients generic
We can now write a function that will just take the ingredient as an argument and decide where to go to fetch it.
Fetching a shopping list
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.
A day at the bakery
Now that we have an efficient way of fetching ingredients, let's handle the orders for the day.
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.
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.
order to ingredients
We create a function that will take an order and return the ingredients needed to complete it.
orders to ingredients
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.
Making one delivery
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.
Adding all recipes
Now that we can bake recipes, let's add the other recipes, cookies and brownies.
Cleaning up order to ingredients
We get rid of the ingredients listed in the definition of order->ingredients and instead look them up in the database.
Ingredients in the database
We need to add a section to the database about where to fetch ingredients and how to use them.
Refactoring scooped, squeezed, and simple
simple? to use the baking database.
Generic fetch ingredient
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
Refactoring fetch-list (again)
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.
Make storage-location more useful
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.
Appendix: Creating a Leiningen project
If you're curious about how the Leiningen project we used was created, I walk through the steps of creating a new project and setting up the bakery dependency.
Guide to baking for the intrepid robot