Most of the Clojure tutorials and Clojure examples on this site require at the minimum Clojure to be installed. Usually, you want Leiningen installed too.
Easy peasy.
Seriously, do it once and ignore it for the rest of your learning experience. Here are the steps for getting Lein and Clojure up and running.
This creates a Clojure web app using the Luminus Framework, Reitit for mapping of incoming requests to the proper handlers, HTTP Kit for HTTP handling, MySQL for JDBC connections to our MySQL database, Swagger for API support, and Buddy for authentication middleware. Your new web app is in the directory,
mywebapp .
Sometimes you need to configure
lein to use a custom JDK. On Linux, set the environment variable
JAVA_CMD to point to the
java executable you want
lein to use.
For example, you might add the following lines to your
.bash_profile
1
2
3
4
5
#used by LEIN
JAVA_CMD=$HOME/installs/jdk-11.0.10+8/bin/java
export JAVA_CMD
Once you update your bash profile, you can start using your configuration by using the following command.
In Clojure, if you want a basic OOP behavior without killing yourself to fully implement Java classes and interfaces in Clojure, the preferred technique is to use records. Sometimes implementing protocols in records suits your needs, mostly due to speed of code execution. However, if you want polymorphism with your records, then multimethods are where it’s at.
Admittedly, multimethods are very flexible, so the technique I’ll show you is one of many techniques you could use. The basic pattern I use for inheritance with records, is to use the
derive command with keywords that are kept as a value in the record. For example,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(defrecordfood[
^Keywordtype
^Stringname
^Stringcolor
^booleanyummy
])
(defnconstruct-food[namecoloryummy]
(->food::foodnamecoloryummy))
(derive::fruit::food)
(defrecordfruit[
^Keywordtype
^Stringname
^Stringcolor
^booleanyummy
^booleanneeds-peeling
])
(defnconstruct-fruit[namecoloryummyneeds-peeling]
(->fruit::fruitnamecoloryummyneeds-peeling))
type is used to track
isa relationships of
::food and
::fruit .
derive is used to set
::food as the parent of
::fruit . Notice the use of custom constructors to set default values for
type in the records.
Most of the time, Clojure does not need locks like Java and other languages. This is because of
atom s,
ref s, and
agent s. I’ll focus on Clojure agents in this short post.
Agents are handled in single-threaded queues. By lining up calls of a function in a queue, no locks are needed. When combined with
send-off , deadlocks won’t happen.
Here’s a quick example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(nsagentexample.core)
(defx(agent0))
(defnmod-x
[z]
(Thread/sleep500)
(incz)
)
(defnfoo
[]
(println"Starting send-offs for agent")
(dotimes[_10]
(send-offxmod-x)
(println"x is now"@x))
(Thread/sleep8000)
(println"final value ... "@x)
(shutdown-agents))
(foo)
A few things are going on here, so let’s look at them.
Want some Object Oriented Programming in Clojure? Here’s a quick example.
Let’s create a dog object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
(nstemp2020toss.dogo)
(defprotocolBark
"Just a protocol"
(barking[this]"bark")
(hungry[this]"hangry")
(sleeping[this]"zzzz"))
(defrecordDog[breed^Integeragecolor]
Bark
(barking[this](str"barking my breed ... "(:breedthis)))
(hungry[this](str"I'm so hungry I could age one whole year to ... "(str(inc(:agethis)))))
(sleeping[this](str"Really tired, and it has nothing to do with my fur color that is "(:colorthis)))
)
Now let’s use the dog object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
(nstemp2020toss.core
(:gen-class)
(:require[temp2020toss.dogo]
[temp2020toss.dogo:refer:all])
(:import(temp2020toss.dogoDog)))
(defn-main
"I don't do a whole lot ... yet."
[&args]
(println"Hello, World!")
(let[my-dog(Dog."chow"8"green")]
(println"-->"(:breedmy-dog))
(println"-->"(barkingmy-dog))))
See, OOP in Clojure is fairly simple. This sample uses protocols with records, but you can add polymorphism with just a little extra work. For polymorphism, use records and multimethods.
I absolutely love the user razvan’s Clojure
defrecord example over at stackoverflow, so I copied it here. I know I’ll never find it again if I don’t. Look at the original over on stackoverflow at this link.
NOTE: All the code in this tutorial can be quickly tested at the REPL using the command
leinrepl to start a REPL to test with.
One of the first things you learn when learning clojure is
print and
println . For example,
1
(println"Hello world!")
That’s probably the first Clojure program you ever wrote. Maybe you wrote this variation.
1
(print"Hello world!")
Because
println or
print are the first ways learned of displaying output in Clojure, most Clojure developers continue using them when debugging code. Nothing wrong with that. But sometimes it is better to use a more specialized tool.
In steps the
prn and
pr commands. These two commands are tailored for printing objects. At first glance, they appear to work the same as
print and
println . However, they actually show more information in some instances.
If using
print or
println , the
\n in the following code quietly gets replaced by the action of a new line being printed.
1
(pr"Hello\nworld!")
When
pr or
prn are used, the two characters
\ and
n are displayed instead of an actual new line. This is very handy when hidden characters are fowling up your layout, or needed to be visible when debugging code.
Final note: The only difference between
pr and
prn , is that
prn prints a new line after the objects it is printing out.
Migratus is a wonderful migration tool for Clojure. You can use it on its own, or if you have a Luminus project, it is already included.
There are a few areas that could use some extra tutorials, though. One of them is code-based migrations. Especially, examples on how to modify the database would be nice.
Here’s a quick explanation of how to modify the database with a code-based migration in Migratus.
First, create an *.edn file in the folder with the regular migrations. It takes the same format as other migrations, so you can really just change the extension of one of your created migrations to ‘.edn’ and change the contents.
You can save a lot of time, money, and frustration by using prewritten libraries and tools when making your full stack apps. One such tool is Redis. Redis is a server that works on most platforms for server-side caching, pub/sub, messaging, Lua scripting, and data structure storage. It is thread-safe, loves puppies and is good with small children.
Seriously, even if you just use Redis as your cache, you’ll save a lot of frustration and make your app scalable.
This tutorial gives an example of using Redis for pub/sub, also known as Publish/Subscribe. All this will be done using Clojure’s Carmine library. (Additional documentation for Carmine found here.