Clojure
A modern Lisp dialect on the JVM
A modern Lisp dialect on the JVM
Clojure is a modern Lisp dialect created by Rich Hickey in 2007, and it runs on the JVM (Java Virtual Machine). Among the Lisp family of languages, which has a 60-year history, it is the most successful modern dialect, and it takes functional programming and immutable data structures as its core philosophy.
Clojure's most distinctive feature is immutability. All data structures are immutable by default, which fundamentally prevents the complex problems that arise in concurrent programming. It uses Persistent Data Structures to provide efficient immutable collections.
Clojure is fully compatible with the JVM ecosystem and can call Java libraries directly. It can also be compiled to JavaScript through ClojureScript and run in the browser, and Babashka provides a fast scripting environment.
In practice, Clojure is used for large-scale data processing, systems where concurrency is important, financial services, and more. It is used in core systems at Nubank (the world's largest digital bank), Walmart, Netflix, and others, and REPL-based development enables fast feedback loops and interactive development.
;; 1. 기본 데이터 구조와 불변성
(def user {:name "홍길동" :age 25 :email "hong@example.com"})
;; assoc은 새로운 맵을 반환 (원본 변경 없음)
(def updated-user (assoc user :age 26))
(println (:age user)) ; 25 (원본 유지)
(println (:age updated-user)) ; 26 (새 맵)
;; 2. 함수형 프로그래밍
(defn process-numbers [numbers]
(->> numbers
(filter even?) ; 짝수만 필터링
(map #(* % %)) ; 제곱
(reduce +))) ; 합계
(println (process-numbers [1 2 3 4 5 6 7 8 9 10])) ; 220
;; 3. 동시성 - Atom을 사용한 안전한 상태 관리
(def counter (atom 0))
(defn increment-counter []
(swap! counter inc))
;; 여러 스레드에서 안전하게 호출 가능
(doall (pmap (fn [_] (increment-counter)) (range 1000)))
(println @counter) ; 1000
;; 4. Java 상호운용
(import '[java.time LocalDateTime]
'[java.time.format DateTimeFormatter])
(defn current-time-formatted []
(let [now (LocalDateTime/now)
formatter (DateTimeFormatter/ofPattern "yyyy-MM-dd HH:mm:ss")]
(.format now formatter)))
(println (current-time-formatted)) ; "2025-01-25 14:30:00"
;; 5. 매크로 - 코드 생성
(defmacro when-valid [condition & body]
`(when ~condition
(try
~@body
(catch Exception e#
(println "오류 발생:" (.getMessage e#))))))
"Concurrency handling is the core of this service, and with Clojure's immutable data structures and STM, safe state management is possible even without locks. We can also use our existing Java libraries as they are."
"Clojure's Persistent Data Structures handle immutable data efficiently through structural sharing. For example, even if you add one element to a vector with a million elements, a new vector is created in O(log n) time, and most of the memory is shared."
"I think we should use ref instead of atom here. We need to coordinate multiple states in a single transaction, but atom only guarantees individual updates. Using ref inside a dosync block makes ACID transactions possible."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Because Clojure runs on the JVM, its cold start time is long. For CLI tools or scripts, use Babashka, or compile with a GraalVM native image.
map, filter, and similar functions are lazily evaluated. If you need side effects, force evaluation with doall or dorun. Otherwise, they may execute at unexpected times.
Clojure's greatest strength is the REPL. Connect your editor to the REPL to evaluate code immediately and develop while inspecting state. This provides a faster feedback loop than TDD.