A comprehensive Clojure cheat sheet for Java developers, covering essential syntax, functions, and best practices for quick reference.
This Clojure cheat sheet is designed to serve as a quick reference guide for Java developers transitioning to Clojure. It covers essential syntax, functions, and best practices, providing a handy resource for developers to consult during their Clojure programming journey. Whether you’re writing your first Clojure program or need a refresher on specific functions, this cheat sheet has you covered.
Clojure code is composed of S-expressions (symbolic expressions), which are lists enclosed in parentheses. These lists can represent function calls, data, or both.
(+ 1 2 3) ; Adds numbers, resulting in 6
Comments in Clojure start with a semicolon (;
). Everything after the semicolon on the same line is ignored by the compiler.
; This is a single-line comment
Use def
to bind a value to a symbol, creating a global variable.
(def pi 3.14159)
Functions are first-class citizens in Clojure. Define them using the defn
macro.
(defn add [a b]
(+ a b))
Clojure provides a rich set of immutable data structures.
Lists are ordered collections, typically used for code (S-expressions).
'(1 2 3 4) ; A list of numbers
Vectors are ordered collections, optimized for random access.
[1 2 3 4] ; A vector of numbers
Maps are collections of key-value pairs.
{:name "Alice" :age 30} ; A map with two key-value pairs
Sets are collections of unique values.
#{1 2 3 4} ; A set of numbers
Clojure supports standard arithmetic operations.
(+ 1 2) ; Addition
(- 5 3) ; Subtraction
(* 2 3) ; Multiplication
(/ 10 2) ; Division
Comparison operators return boolean values.
(= 1 1) ; Equality
(not= 1 2) ; Inequality
(< 1 2) ; Less than
(> 2 1) ; Greater than
(<= 1 2) ; Less than or equal to
(>= 2 1) ; Greater than or equal to
Logical operators work with boolean values.
(and true false) ; Logical AND
(or true false) ; Logical OR
(not true) ; Logical NOT
Clojure provides several ways to handle conditional logic.
(if true "yes" "no") ; Basic if expression
(cond
(< 1 2) "less"
(> 1 2) "greater"
:else "equal") ; Cond expression for multiple conditions
Clojure emphasizes recursion over traditional loops.
(loop [i 0]
(when (< i 10)
(println i)
(recur (inc i)))) ; Loop and recur for iteration
Functions are defined using defn
.
(defn greet [name]
(str "Hello, " name))
Anonymous functions are created using fn
or the shorthand #()
.
(fn [x] (* x x)) ; Anonymous function
#(* % %) ; Shorthand for the same
Functions that take other functions as arguments or return them.
(map inc [1 2 3]) ; Applies inc to each element
(filter even? [1 2 3 4]) ; Filters even numbers
(reduce + [1 2 3 4]) ; Sums the elements
Clojure’s data structures are immutable, meaning they cannot be changed once created. Instead, operations on these structures return new versions with the desired changes.
Clojure uses structural sharing to efficiently manage memory when creating new versions of data structures.
(def original [1 2 3])
(def modified (conj original 4)) ; Adds 4, creating a new vector
Namespaces help organize code and manage dependencies.
(ns my-app.core) ; Declare a namespace
Use require
to include other namespaces.
(require '[clojure.string :as str]) ; Alias clojure.string as str
Clojure runs on the JVM and can seamlessly interact with Java code.
Use the .
operator to call Java methods.
(.toUpperCase "hello") ; Calls Java String method
Use new
to create Java objects.
(new java.util.Date) ; Creates a new Date object
map
, reduce
, and filter
for concise and expressive code.criterium
to measure performance and identify bottlenecks.This cheat sheet provides a comprehensive overview of Clojure’s syntax and core functions, serving as a valuable resource for Java developers learning Clojure. By understanding these fundamental concepts, developers can effectively leverage Clojure’s powerful features in their projects.