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!

pr and prn

NOTE: All the code in this tutorial can be quickly tested at the REPL using the command lein repl  to start a REPL to test with.

One of the first things you learn when learning clojure is print  and println . For example,

That’s probably the first Clojure program you ever wrote. Maybe you wrote this variation.

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.

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.

 

Private Functions in Clojure

Clojure developers coming from Object Oriented backgrounds know how useful it is to separate implementation details from functions and methods that are supposed to be used by other coders. Java has public and private methods. This allows devs to limit usage and visibility of functions that are not meant to be used directly.

There are two options in Clojure for keeping functions private. First, you can use a special macro, defn- . defn-  works just like defn , except that the function is only visible inside the current namespace. This makes the function private in the Java sense to the term. Continue reading “Private Functions in Clojure”

Clojure REPL for Development

A common way to develop code (don’t blame the messenger) is to start the application watch for the logged errors, make changes and then restart the application again, looking for more logged errors.

That’s not how it’s done in the world of Clojure, not just because it’s bad, but because the process of starting any Clojure program is horribly slow. Instead, in Clojure normally you start the application and then connect with a terminal window running a Clojure REPL. For example, if you are running a Luminus server, when you start it, it will tell you what port the server is listening for REPL connections on.

Continue reading “Clojure REPL for Development”

Clojure Destructuring with Functions

In clojure terms, destructuring is taking apart a sequence and putting the pieces you care about into variables you can more easily work with. Destructuring involves square brackets that make it look like a vector, but it is really binding a variable from a given seq to a new variable. Let’s look at an example involving a function in Clojure.

Notice the -main function has three variables in a vector: a site address, a web page, and a user name. It is much easier in the new function to unpackage these variables form the vector and bind them to variables that have meaningful names, and then use the names.

The result is as follows.

What if we have more values in the seq than values we intend to bind to? For example, below we added an additional index with the words “random stuff”.

Continue reading “Clojure Destructuring with Functions”