Skip to content

Instantly share code, notes, and snippets.

Tim McCormack timmc

Block or report user

Report or block timmc

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:5049288
(defmacro builder [initial method & argsets]
(list* 'doto initial
(for [args argsets]
`(~method ~@args))))
;; Example:
(builder (StringBuilder.) .append [1] ["a"])
;; becomes...
View gist:5040043
(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 Oct 9, 2012 — forked from thiagofm/fib2.clj
fib clojure 2
View fib2.clj
(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 Oct 9, 2012 — forked from thiagofm/fib.clj
fib clojure
View fib.clj
;; 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 Jul 2, 2012
Factorial in Clojure without using alphanumeric characters
View gist:3036114
(#(% [% (+ (*)(*)(*)(*)(*)(*)(*))])
#(if (= (% (*)) (+))
(*)
(* ((% (+))
[(% (+)) (- (% (*)) (*))])
(% (*)))))
;; => 5040
;; Almost there, just an "if" to get rid of...
(#((% (+(*))) %) ;; call arg 1 with all args
View gist:2338038
(import '(java.io ByteArrayInputStream ByteArrayOutputStream
ObjectInputStream ObjectOutputStream))
(defmacro wtf
[]
(Boolean. false)) ;; note the lack of syntax-quote
---------
user=> (wtf)
View gist:1696578
(defmacro def+keep-meta [name a-var]
`(alter-meta! (def ~name ~a-var) merge (meta ~a-var)))
View 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)))
You can’t perform that action at this time.