When coming from other languages, devs often think the best way to check for an empty vector or other sequence in Clojure is to either check the count is less than one or check for (not (empty? some-seq)) . Both will work, but are not idiomatic, meaning “Your newbness is showing.” Continue reading “Checking for Empty Seqs in Clojure”
Month: July 2020
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.
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.
1 2 3 4 5 |
(defn request-printer [[site page user]] (println (str "site=>" site "\npage=>" page "\nuser=>" user))) (defn -main [& args] (request-printer ["example.com" "clojure.html" "clojure dude"])) |
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.
1 2 3 |
site=>example.com page=>clojure.html user=>clojure dude |
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”.
1 2 3 4 5 |
(defn request-printer [[site page user]] (println (str "site=>" site "\npage=>" page "\nuser=>" user))) (defn -main [& args] (request-printer ["example.com" "clojure.html" "clojure dude" "random stuff"])) |
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.
Clojure with a Touch of Redis
If you’ve ever done enterprise web application development, you’ve heard of Redis. Redis has a lot of uses, but the most common is as replacement for your standard session. Even just using it’s key-value store (think hashmap on steroids), Redis provides so much power that many enterprise sites integrate it into their larger projects.
Lucky for those of us using Clojure, Redis integrates nicely using Carmine, an open source Clojure library for interacting with Redis.