Run tests from Clojure Repl and Leiningen

Start a REPL with lein repl, then use require (require ‘[clojure.test :refer [run-tests]]) (require ‘your-ns.example-test :reload-all) (run-tests ‘your-ns.example-test) I prefer to stay in the user namespace, as opposed to changing it with in-ns as mentioned by another answer. Instead, pass the namespace as an argument to run-tests (as shown above). I’d also recommend staying away … Read more

Order of code in Clojure

You can use forward declarations in Clojure so you can call functions that haven’t been defined yet. (declare readCmd) should work! In Clojure, the order in which you define functions is important, a function can’t call another function (or anything for that matter) that hasn’t been defined yet. That’s why we have forward declarations.

What is the idiomatic way to assoc several keys/values in a nested map in Clojure?

Here are a couple of ways. user> (update-in person [:name] assoc :first-name “Bob” :last-name “Doe”) {:name {:middle-name “Michael”, :last-name “Doe”, :first-name “Bob”}} user> (update-in person [:name] merge {:first-name “Bob” :last-name “Doe”}) {:name {:middle-name “Michael”, :last-name “Doe”, :first-name “Bob”}} user> (update-in person [:name] into {:first-name “Bob” :last-name “Doe”}) {:name {:middle-name “Michael”, :last-name “Doe”, :first-name “Bob”}} user> … Read more

Profiling tool for Clojure?

I found VisualVM (see here and here) to be very convenient. Its use with Clojure has been described about a year ago in this blog post; as far as I can see, it’s not outdated in any way. Note that the GUI from which one starts the VisualVM profiler has a prominent text area where … Read more

clojure convert lazy-seq to hash map

To answer your actual question: (apply hash-map record) But since you actually already have separate keys and values, I would suggest skipping the interleave step and instead writing (zipmap fields values) Or if you have your heart set on into, you could (into {} (map vector fields values))

Remove nil values from a map?

your for list comprehension returns a LIST of maps, so you need to APPLY this list to the merge function as optional arguments: user> (apply merge (for [[k v] record :when (not (nil? v))] {k v})) {:b 2, :a 1} More concise solution by filtering the map as a sequence and conjoining into a map: … Read more

Compojure routes with different middleware

(defroutes user-routes* (GET “-endpoint1” [] (“USER ENDPOINT 1”)) (GET “-endpoint2” [] (“USER ENDPOINT 1”))) (def user-routes (-> #’user-routes* (wrap-basic-authentication user-auth?))) (defroutes admin-routes* (GET “-endpoint” [] (“ADMIN ENDPOINT”))) (def admin-routes (-> #’admin-routes* (wrap-basic-authentication admin-auth?))) (defroutes main-routes (ANY “*” [] admin-routes) (ANY “*” [] user-routes) This will run the incoming request first through admin-routes and then through … Read more

Good Clojure Code Examples? [closed]

Prominent open source Clojure projects: Compojure – a web framework (since you mentioned Spring) Ring – HTTP abstraction API Incanter – R-like statistical computing and graphics environment Leiningen – a build tool for Clojure Clojure-Contrib – you mentioned the core library, but contrib is definitely worth looking at as well. Enclojure – Clojure IDE tools … Read more