Clojure

From G1Joshi/Agent-Skills · View interactive version

Clojure is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Clojure programming assistance covering immutable data structures, macros, software transactional memory (STM), and REPL workflows. Use when building concurrent functional systems or data transformation pipelines.

How to use this skill

Skill file

Clojure

A Lisp hosted on the JVM (and JS via ClojureScript) with a focus on immutability.

When to Use

Quick Start

(println "Hello, World!")

(defn square [x]
  (* x x))

(map square [1 2 3]) ; (1 4 9)

Core Concepts

Persistent Immutable Data Structures

Clojure vectors, maps, and sets use structural sharing to provide immutable snapshots with O(log32 N) performance:

;; Creating and updating immutable maps via structural sharing
(def original-user {:id 415 :name "Jane" :roles #{:member}})
(def updated-user (update original-user :roles conj :admin))

;; original-user remains completely unchanged:
;; {:id 415 :name "Jane" :roles #{:member}}

Concurrency Primitives (Atoms & Software Transactional Memory)

Manages shared state transitions safely without manual lock synchronization:

;; Atoms provide atomic, lock-free state updates via compare-and-swap
(def request-count (atom 0))

;; Thread-safe atomic increment
(swap! request-count inc)
(println "Current requests:" @request-count)

Threading Macros (`->` and `->>`)

Pipelines data transformations cleanly without deeply nested function calls:

(defn calculate-revenue [orders]
  (->> orders
       (filter :completed?)
       (map :total-cents)
       (reduce +)
       (* 0.01)))

Common Patterns

Threading Macros for Data Transformation

Problem: Deeply nested function calls are hard to read and mentally trace backwards.

Solution:

Use ->> (thread-last) macro for sequential transformations:

(defn process-orders [orders]
  (->> orders
       (filter #(= (:status %) "completed"))
       (map :amount)
       (reduce + 0)))

(println (process-orders [{:status "completed" :amount 50}
                          {:status "pending" :amount 20}
                          {:status "completed" :amount 30}]))
;; => 80

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :------------------------------------------------------ | :----------------------------------------------------------- | :------------------------------------------------------------------------ |

| CompilerException java.lang.ClassCastException | Passing wrong data structure (e.g. sequence instead of map). | Verify types using (type x) in the REPL. |

| ArityException: Wrong number of args | Function called with more or fewer arguments than defined. | Check function parameter vector and multi-arity definitions. |

| OutOfMemoryError: Java heap space with lazy sequences | Retaining the head of an unbounded lazy sequence. | Use dorun or doseq to consume lazy seqs without retaining references. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.