Learn Clojure Quick

If you need to understand basics of Clojure quickly …

Watch these videos

Clojure for Java Programmers part 1
https://youtu.be/P76Vbsk_3J0?si=oWDHQwjHWt8qvbpT

Clojure for Java Programmers part 2
https://youtu.be/hb3rurFxrZ8?si=sUsf_67h3EqC_e-I

Simple Made Easy
https://youtu.be/LKtk3HCgTa8?si=9K4sZvThdJKJQhad

Clojure Concurrency
https://youtu.be/dGVqrGmwOAw?si=Twr5pfeJtgMH-FM4

 

Use these reference sites

Official Clojure Home Page
https://clojure.org/

ClojureDocs
https://clojuredocs.org/

Leiningen
https://leiningen.org/

Clojure Web Development
https://luminusweb.com/

Choose code puzzles to practice with from the following sites

Project Euler
https://projecteuler.net/

Code Golf
https://codegolf.stackexchange.com/

Advent of Code
https://adventofcode.com/2023/events

Clojurcises: hash-map

Good practice. Use each of the following functions and macros to create or manipulate hash-maps in Clojure.

{}
hash-map

get
get-in (one keyword)
get-in (<map> [<key1> <key2>])

(<map> <key>)
(:<keyword> <map>)
(:<kw1> (:<kw2> <map>))

(-> <map> :<k1>)
(-> <map> :<k1> :<k2>)

assoc (change value)
assoc-in

assoc (add new key-value pair)
assoc-in

update (function only)
update (function with args)

update-in (function only)
update-in (function with args)

Clojure Docker Web Apps

If you’re interested in making a commercial grade Clojure web app that deploys as a Docker container, this is the tutorial for you.

Prerequisites: You will need Leiningen and Docker installed.

First step needed is to create a Clojure web app template project. I used the following command.

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 .

Continue reading “Clojure Docker Web Apps”

JAVA_CMD for lein

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

Once you update your bash profile, you can start using your configuration by using the following command.

 

Clojure Record Heirarchy

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,

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.

Continue reading “Clojure Record Heirarchy”

Clojure Agents

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.

A few things are going on here, so let’s look at them.

Continue reading “Clojure Agents”

Quick OOP Example

Want some Object Oriented Programming in Clojure? Here’s a quick example.

Let’s create a dog object.

Now let’s use the dog object.

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.

Razvan’s ‘defrecord’ Example

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.

Here’s the example …

Clojure defrecord example:

If you’re looking for more information on combining protocols with records in Clojure, then Matthew Boston has the blog for you!