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
(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 |
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
(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)) |
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
(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 |
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
(apply map vector [[1 2 3] [4 5 6] [7 8 9]]) | |
;=> ([1 4 7] [2 5 8] [3 6 9]) |
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 inner-product [& args] (reduce + (apply map * 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
;; 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))) |
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 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))))) |
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
(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"])) |
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
(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) |
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
(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) |