Skip to content

Instantly share code, notes, and snippets.

View emdeesee's full-sized avatar
🤔
hmm

Michael Cornelius emdeesee

🤔
hmm
View GitHub Profile
@emdeesee
emdeesee / probeseq.clj
Last active January 6, 2016 19:26
My solution to 4clojure problem #65 (http://www.4clojure.com/problem/65)
(fn [coll]
(let [x [:hoopy :frood]]
;; If we add x twice, but count increases by only 1
;; the collection must be associative.
(if (= (inc (count coll)) (count (into coll [x x])))
;; associative - if coll is a map, the first element of our known
;; sequence will become a key when the sequence is conj-ed to the
;; collection
@emdeesee
emdeesee / merge-w-fn.clj
Last active August 28, 2015 14:47
My solution to 4clojure problem 69 (http://www.4clojure.com/problem/69)
(fn [f m & ms]
(reduce
(fn [m n]
(reduce
(fn [res [k v]]
(assoc res k
(if-let [v' (res k)]
(f v' v)
v)))
m n))
@emdeesee
emdeesee / best_hand.clj
Created September 4, 2015 22:14
My solution to 4clojure problem #178 (http://www.4clojure.com/problem/178)
(fn [hand]
(let [[suits ranks] (apply map vector hand)
flush (apply = suits)
straight (let [vs (sort (map (zipmap "A23456789TJQK" (drop 1 (range))) ranks))]
(some (->> vs cycle (partition 5 1) (take 2) set)
(->> (range 1 14) cycle (partition 5 1) (take 10))))
counts (->> ranks frequencies vals sort)]
(cond
(and flush straight) :straight-flush
(= counts [1 4]) :four-of-a-kind
@emdeesee
emdeesee / transpose.clj
Created September 14, 2015 18:51
Idiom for transposing a sequence of sequences in clojure
(apply map vector [[1 2 3] [4 5 6] [7 8 9]])
;=> ([1 4 7] [2 5 8] [3 6 9])
@emdeesee
emdeesee / x.clj
Created September 29, 2015 19:50
Functional inner product - #LNKCoffeeCode "Backus to the future..."
(defn inner-product [& args] (reduce + (apply map * args)))
@emdeesee
emdeesee / slurp.lisp
Created February 5, 2016 15:27
Clojure slurp in Common Lisp
;; This is not a complete implementation of slurp, since it doesn't handle URLs, just paths.
(defun slurp (path)
(with-open-file (stream path)
(let ((data (make-string (file-length stream))))
(read-sequence data stream)
data)))
@emdeesee
emdeesee / lazylines.clj
Created September 29, 2016 19:21
Get lines from a file, lazily.
(defn lazy-lines [file]
(letfn [(aux [rdr]
(lazy-seq
(if-let [line (.readLine rdr)]
(cons line (aux rdr))
(.close rdr))))]
(lazy-seq
(aux (clojure.java.io/reader file)))))
@emdeesee
emdeesee / days_of_xmas.clj
Last active December 23, 2016 23:36
12 Days of Christmas in 538 Characters of Clojure
(reduce #(let[[d g]%2 p(cons g %)](doseq[l(cons d p)](println l))(newline)(if % p(list(apply str"and "g))))nil(map vector(map #(format"On the %s day of Christmas my true love gave to me"%)'(first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth))["a partridge in a pear tree""two turtle doves""three french hens""four calling birds""five golden rings""six geese a-laying""seven swans a-swimming""eight maids a-milking""nine ladies dancing""ten lords a-leaping""eleven pipers piping""twelve drummers drumming"]))
@emdeesee
emdeesee / atti.lisp
Last active March 17, 2017 19:18
Acing the Technical Interview. Inspired by https://aphyr.com/posts/340-acing-the-technical-interview, an exercise in Common Lisp
(defun pros (head tail)
(lambda (m) (funcall m head tail)))
(defun kar (l)
(funcall l (lambda (head _) (declare (ignore _)) head)))
(defun kdr (l)
(funcall l (lambda (_ tail) (declare (ignore _)) tail)))
(defun mth (l m)
@emdeesee
emdeesee / hashy.lisp
Created March 31, 2017 14:11
Before I learned of the alexandria library, I rolled my own hash table deconstruction. Preserved here because of my inordinate pride.
(defun hash-table-unzip (table)
(with-hash-table-iterator (iter table)
(labels ((aux (keys values)
(multiple-value-bind (more? k v) (iter)
(if more?
(aux (cons k keys) (cons v values))
(values keys values)))))
(aux nil nil))))
(defun hash-table-keys (table)