Clojure Cheat Sheet



There is a test suite in clojure-cheatsheet-tests.el that checks that every symbol defined in the cheatsheet is valid, and that every symbol defined by Clojure is in the sheet (with a few explicit exceptions). To run the test suite: Clone the project. Open clojure-cheatsheet-tests.el and eval the whole file. Get A Weekly Email With Trending Projects For These Topics. Unsubscribe easily at any time.

Table of Contents

  • Vectors
  • Maps
Clojure

When I try to write or read some Clojure code, every now and then Iget stuck on some destructuring forms. It’s like a broken record. Onemoment I’m in the zone, then this thing hits me and I have to stopwhat I’m doing to try and grok what I’m looking at.

Clojure Cheat Sheet Printable

So I decided I’d write a little tutorial/cheatsheet for Clojuredestructuring, both as an attempt to really grok it (I absorb stuffmore quickly if I write it down), and as future reference for myselfand others.

Below is the whole thing, copied from the original [gist][gist]. I’mplanning on adding more (elaborate) examples and a section forcompojure’s own destructuring forms. If you want tobookmark the cheat sheet, I recommend the gist since it hasproper syntax highlighting and will be updated first.

Simply put, destructuring in Clojure is a way extract values from adatastructure and bind them to symbols, without having to explicitlytraverse the datstructure. It allows for elegant and concise Clojurecode.

Vectors

Syntax:[symbol another-symbol] ['value' 'another-value']

You don’t have to match the full vector.

You can use & rest to bind the remaining part of the vector to rest.

When a destructuring form “exceeds” a vector (i.e. there not enoughitems in the vector to bind to), the excess symbols will be bound tonil.

Clojure cheat sheet pdf

You can use :as some-symbol as the last two items in thedestructuring form to bind the whole vector to some-symbol

You can use both & rest and :as some-symbol.

Optional arguments for functions

With destructuring and the & rest form, you can specify optionalarguments to functions.

Maps

Syntax:{symbol :key, another-symbol :another-key} {:key 'value' :another-key 'another-value'}

Similar to vectors, if a key is not found in the map, the symbol willbe bound to nil.

Sheet

Clojure Cider Cheat Sheet

2020

You can provide an optional default value for these missing keys withthe :or keyword and a map of default values.

Cheat

The :as some-symbol form is also available for maps, but unlikevectors it can be specified anywhere (but still preferred to be thelast two pairs).

And combining :as and :or keywords (again, :as preferred to be the last).

There is no & rest for maps.

Shortcuts

Having to specify {symbol :symbol} for each key is repetitive andverbose (it’s almost always going to be the symbol equivalent of thekey), so shortcuts are provided so you only have to type the symbolonce.

Here are all the previous examples using the :keys keyword followedby a vector of symbols:

There are also :strs and :syms Brew on macos catalina. alternatives, for when your map hasstrings or symbols for keys (instead of keywords), respectively.

Clojure Cheat Sheet Pdf

Keyword arguments for function

Map destructuring also works with lists (but not vectors).

Clojure Cheat Sheet

This allows your functions to have optional keyword arguments.