đŸ„· Clojure Pro Tip 5: Hiccup Raw

In case you don’t know it, we can use raw strings, like embedding JS code, in hiccup. I just found out we can use raw to prevent strings from getting escaped. I used to have to define a dedicated app.js for that, which would need an extra HTTP request. Hiccup raw in action Looking back, I should’ve found this at the very beginning, as it’s just mentioned on its GitHub homepage, but somehow I missed it. [Read More]

đŸ„· Clojure Pro Tip 4: Cider ClojureDocs

Sometimes, we may want a few examples of a Clojure API for inspiration while developing, especially when reading other people's or open source code. With Cider in Emacs, we can run M-x cider-doc to see the docstring for the symbol. We can even use M-x cider-clojuredocs for some examples if the symbol is from the language core. Running M-x cider-clojuredocs I happened to find this command recently. I bet I was not the only one who didn't know it, thus I'm sharing. [Read More]

đŸ„· Clojure Pro Tip 3: the Thread-As Macro

So you're more than excited to find out how Clojure's thread macros can make code clean and concise, you just love it! But at some point you'll be trapped a bit as the threaded argument's positions are inconsistent using -> or ->>.

Luckily, you can use as-> to place the argument wherever you like:

Lambda vs. thread-as
Lambda vs. thread-as

đŸ„· Clojure Pro Tip 2: group-by

It's amazingly easy to group things in Clojure. The language core provides a group-by function directly. Together with many built-in functions or key functions, it creates a ton of possibilities, and we can also bake our grouping function if none is satisfying.

group-by
group-by

Example from Clojure Koans.

Collecting Events to Google Analytics using ClojureScript

When you’re building a side project, chances are you need to collect user events so that you can understand user behaviors better and eventually improve your product. So recently I made a small ClojureScript library for that, it supports web apps or chrome extensions. Check it out at https://github.com/whatacold/google-analytics, it also contains a demo project using shadow-cljs as the build tool. Hope it helps, and give it a star ⭐ on GitHub and share it if it does, thanks! [Read More]

đŸ„· Clojure Pro Tip 1: the Discard Reader Symbol

In Clojure, we can use comment (aka Rich Comment Blocks) for tests or experiments in development. However, since a comment evaluates to nil, you may run into surprising results or even errors if you misuse it.

Comment vs. Discard
Comment vs. Discard

In these scenarios, you may tend to use ; to comment them out, but a better choice is to use the discard reader symbol #_.

Constructing Clojure Maps Conditionally

I was wondering how to construct a multi-key map conditionally while I was coding in Clojure. Ideally, I would like to build it "in one pass" like {:bar 2 (when true :baz 3)}, but from what I had collected from Blue Sky, it seemed that's impossible, or it's just not an idiomatic way to program like that in Clojure. (Or, is this just a side effect of writing too much imperative code? [Read More]

A Few Quick Notes About babashka/fs

Recently I've used babashka/fs a little bit, here are some quick notes for it: Path vs File. Use Path whenever possible, according to this SO answer to "Java: Path vs File". This is actually Java related. It's ok to use a path as a key for a clojure map. At my first try, I somehow came to the conclusion that it's not ok, while I was refactoring the live reload for clay. [Read More]

Doing Unit test in Clojure Is Easy

While refactoring the live reload feature of Clay, I realized I'd better break long functions into smaller and functional ones (as many as I can), which is also a common practice in the clojure community. Small pure functions not only are easy to verify on the development process (using a REPL), but also are easy to test. And unit tests are easy to write in clojure, just use deftest from clojure. [Read More]

Adding Clojure Dependencies from the Command Line

While debugging a live reload issue of Clay a few days ago, I learned from Markus Agwin that we can actually add dependencies on the fly from the command line, just like this: clj -Sdeps "{:deps {org.scicloj/clay {:mvn/version \"2-beta21\"}}}". Even better, we can add dependency from a Git repo and specify the commit hash: clj -Sdeps "{:deps {io.github.scicloj/clay {:git/url \"https://github.com/scicloj/clay.git\" :git/sha \"35a46541db66ae6b4e1af628b7c24c42d3be418f\"}}}", which is really helpful for experimenting and verifying things. [Read More]
Clojure  Tweet  clj