This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro builder [initial method & argsets] | |
(list* 'doto initial | |
(for [args argsets] | |
`(~method ~@args)))) | |
;; Example: | |
(builder (StringBuilder.) .append [1] ["a"]) | |
;; becomes... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn write-builder [cube & ats] | |
(loop [builder (WriteBuilder. cube) | |
ats ats] | |
(if (empty? ats) | |
builder | |
(recur (.at builder# (first ats#) ...missing-arg...) | |
(rest ats#))))) | |
(write-builder cube [[time (now)]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn fib | |
([n] | |
(fib [0 1] (- n 2))) | |
([list-fib n] | |
(if (zero? n) | |
list-fib | |
(recur (conj list-fib (apply + (take-last 2 list-fib))) | |
(dec n))))) | |
(fib 10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Changes: | |
;; - use + instead of reduce + | |
;; - only calculate (+ actual-n next-n) once per iteration | |
;; - No dangling closing parens | |
;; - dec instead of (- ... 1) (to taste) | |
;; - zero? instead of (= ... 0) | |
;; - reverse the if clauses to take care of the base case first | |
(defn fib-list | |
[actual-n next-n iterations-left full-list] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(#(% [% (+ (*)(*)(*)(*)(*)(*)(*))]) | |
#(if (= (% (*)) (+)) | |
(*) | |
(* ((% (+)) | |
[(% (+)) (- (% (*)) (*))]) | |
(% (*))))) | |
;; => 5040 | |
;; Almost there, just an "if" to get rid of... | |
(#((% (+(*))) %) ;; call arg 1 with all args |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(import '(java.io ByteArrayInputStream ByteArrayOutputStream | |
ObjectInputStream ObjectOutputStream)) | |
(defmacro wtf | |
[] | |
(Boolean. false)) ;; note the lack of syntax-quote | |
--------- | |
user=> (wtf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro def+keep-meta [name a-var] | |
`(alter-meta! (def ~name ~a-var) merge (meta ~a-var))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; This evaluates properly... | |
(defn barr= | |
([x] true) | |
([^bytes x ^bytes y] | |
(java.util.Arrays/equals x y)) | |
([x y & more] | |
(if (barr= x y) | |
(if (next more) | |
(recur y (first more) (next more)) | |
(barr= y (first more))) |