;; Using :validator to replace the `if` (def a (atom 3 :validator pos?)) (swap! a dec) ;; Without using `:validator`, we need to put a `if` (def b (atom 3)) (swap! b (fn [n] (if (pos? (dec n)) (dec n) (throw (ex-info "content must be positive" {:a 1})))))
Wednesday, November 22, 2023
Clojure 取代 if 的技巧 (3) --- Atom validator
Labels:
clojure
Wednesday, November 15, 2023
*out* 被 captured 時的變通之道
在 Clojure , *out* 是 System/out 這個變數被 java.io.OutputStreamWriter 的物件。
有的時候,有一些 codebase ,*out* 會被 captured ,這種時候,無論怎麼 print ,都無法 print 成功。這種時候,簡單的變通之道是:
(add-tap (fn [o] (.println System/out o)))
(tap> DATA)
Labels:
clojure
Subscribe to:
Posts (Atom)