Plain Text Email from Clojure

I recommend using the Apache Commons Email library when sending email from Clojure apps. Here’s a simple example.

First up, you need to add the dependency to ‘project.clj’

Next, your code would look something like the following. Caution, … This will send emails, so configure it to send the emails to a personal email address for testing purposes.

 

Clojure UTC DateTime

When saving datetimes to databases like MySQL, it is best to save them as UTC instead of using your local timezone. That way, if your company, or IT department, or servers move to a new timezone, you won’t have to worry about adjusting every time stored in the database to match the new timezone. The trick is that you always return UTC times in your APIs, and then expect the clients or tools using your APIs to convert the provided UTC time to their local timezone for display.

This leads to a common question. How do I get the current datetime in UTC when using Clojure?

The formatted string with the UTC time will be yyyy-MM-dd hh:mm:ss.

Clojure Migratus: “Migration reserved by another instance”

Migratus is a great migration tool for Clojure enterprise projects. Migrations allow you to do and undo changes to a database very quickly. The best use case for Migratus is update of a database for your enterprise project or setting up a new database for a development environment. Setting up the database can be on a production server or your local development environment.

Eventually one of your migrations is going to have an SQL exception. Normally, SQL exceptions aren’t a big deal. You just fix it, and run the migration again. However, Migration gets into an :ignore state and gives you an exception reading …

… and then refuses to run another migration no matter what you do.

The fix is to go into your ‘schema_migrations’ table in your database and remove the row that has an id of -1.

That’s really a simple fix for your favorite Clojure migration tool.

 

Structure and Interpretation of Computer Programs

The book, Structure and Interpretation of Computer Programs, is considered one of the top necessary reads for software engineers. It is a fairly expensive book to buy hard copies of. Luckily, the authors generously provide a PDF version for free. Currently, you can download the free version from the sicp-pdf on GitHub. Also, MIT offers a free download at https://web.mit.edu/alexmv/6.037/sicp.pdf.

Structure and Interpretation of Computer Programs is written in Scheme, but is all applicable to Clojure. Functional programming is taught thoroughly, along with lazy sequences and higher order functions.

Structure and Interpretation of Computer Programs doesn’t ignore the other side of the fence. It also goes over objects, state, and mutable data.

This is a Computer Science book, so expect some deep thinking and descriptions. But, if you’re looking to improve your software engineering skills, give reading this book a go.

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”

Clojure Redis: Get and Set

For a brief intro to setting up Redis with Clojure using Carmine, see my earlier post on setting up Redis with Clojure.

The most common killer use for Redis is as a key-value store. You simply get and set values in it like in a map. It’s very handy for site wide session storage with web apps, or even in more traditional applications.

Continue reading “Clojure Redis: Get and Set”