Skip to content

Instantly share code, notes, and snippets.

View timmc's full-sized avatar

Tim McCormack timmc

View GitHub Profile
@timmc
timmc / gist:5049288
Last active December 14, 2015 07:18 — forked from anonymous/gist:5049270
(defmacro builder [initial method & argsets]
(list* 'doto initial
(for [args argsets]
`(~method ~@args))))
;; Example:
(builder (StringBuilder.) .append [1] ["a"])
;; becomes...
@timmc
timmc / gist:5040043
Last active December 14, 2015 06:08 — forked from dcolish/gist:5039976
(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)]])
@timmc
timmc / fib2.clj
Created October 9, 2012 22:43 — forked from thiagofm/fib2.clj
fib clojure 2
(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)
@timmc
timmc / fib.clj
Created October 9, 2012 21:53 — forked from thiagofm/fib.clj
fib clojure
;; 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]
@timmc
timmc / gist:3036114
Created July 2, 2012 22:22
Factorial in Clojure without using alphanumeric characters
(#(% [% (+ (*)(*)(*)(*)(*)(*)(*))])
#(if (= (% (*)) (+))
(*)
(* ((% (+))
[(% (+)) (- (% (*)) (*))])
(% (*)))))
;; => 5040
;; Almost there, just an "if" to get rid of...
(#((% (+(*))) %) ;; call arg 1 with all args
(import '(java.io ByteArrayInputStream ByteArrayOutputStream
ObjectInputStream ObjectOutputStream))
(defmacro wtf
[]
(Boolean. false)) ;; note the lack of syntax-quote
---------
user=> (wtf)
(defmacro def+keep-meta [name a-var]
`(alter-meta! (def ~name ~a-var) merge (meta ~a-var)))
@timmc
timmc / inline.clj
Created January 2, 2012 18:41 — forked from sritchie/inline.clj
;; 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)))