Skip to content

Instantly share code, notes, and snippets.

@rxacevedo
rxacevedo / xpath_scraping.clj
Last active August 29, 2015 13:56
Scraping via XPath
;; xalan 2.6 gets put on the classpath when using
;; Incanter, causes issues with clj-xpath. So DON'T
;; use Incanter when using clj-xpath.
(->> (ClassLoader/getSystemClassLoader)
(.getURLs)
(map #(.toString %))
(clojure.string/join "\n")
(re-seq #".*xalan.*")
(pprint))
@rxacevedo
rxacevedo / hiccup_example.clj
Created February 26, 2014 18:00
HTML markup via hiccup in Clojure
; This
(html [:html
[:head
[:meta {:charset "utf-8"}]
[:title "Title"]]
[:body
[:header
[:h1 "H1 header!"]]
[:section
[:ul
@rxacevedo
rxacevedo / lazy-map.clj
Created April 4, 2014 20:22
Lazy map implementation for use when doing side-effectual things by mapping some fn with side-effects to a seq.
(defn lazy-map [f coll]
(if (seq coll)
(lazy-seq (cons (f (first coll)) (lazy-map f (rest coll))))
nil))
;; The difference
clj-print.core> (type (seq (map print (range 50))))
012345678910111213141516171819202122232425262728293031
clojure.lang.ChunkedCons
@rxacevedo
rxacevedo / binomial_cfs.clj
Last active August 29, 2015 13:58
Binomial coefficients
;; Grouped by n (0 choose k, 1 choose k, 2 choose k ... 19 choose k)
;; Again, making it harder than it needs to be
(let [binomial-cfs (for [n (range 20)
k (range (inc n))]
{:n n
:k k
:nCk (choose n k)})]
(doseq [n (into #{} (map :n binomial-cfs))]
(println (map :nCk (filter #(= n (:n %)) binomial-cfs)))))
(pprint (take 500 (iterate (fn [fib] (+ 1 (/ 1 fib))) 1)))
; (1
; 2
; 3/2
; 5/3
; 8/5
; 13/8
; 21/13
; 34/21
; 55/34
@rxacevedo
rxacevedo / merge_sort.clj
Last active August 29, 2015 14:01
Merge sort in Clojure (probably not optimal)
;; Quicky Merge sort and test
(defn merge-sort [coll]
(letfn [(sort [coll]
(if (= 1 (count coll)) coll
(let [sz (count coll)
left (take (/ sz 2) coll)
right (drop (/ sz 2) coll)]
(merge (sort left) (sort right)))))
(merge [left right]
(
(fn [x] (list x (list (quote quote) x))) ;; yields (x (quote x))
(quote (fn [x] (list x (list (quote quote) x)))) ;; yields (fn [x] (list x (list (quote quote) x)))
) ;; so the whole thing, via substituion, yields ((fn [x] (list x (list (quote quote) x))) (quote (fn [x] (list x (list (quote quote) x)))))
@rxacevedo
rxacevedo / reify.clj
Created June 3, 2014 15:58
Reify example
(let [is-dir (memfn isDirectory)
ff (reify java.io.FileFilter
(accept [this f]
(is-dir f)))]
(pprint
(seq
(.listFiles (java.io.File. "C:/") ff))))
;; (#<File C:\$Recycle.Bin>
;; #<File C:\apache-ant-1.7.1>
;; #<File C:\Applications>
@rxacevedo
rxacevedo / 2.clj
Last active August 29, 2015 14:02
Pascal's triangle and powers of 2
(print-table
(for [n (range 15)
:let [row (map #(choose n %) (range (inc n)))]]
{:row (with-out-str (print row))
:sum (reduce + row)
:binary (cl-format nil "~b" (bit-shift-left 2r00000001 n))}))
; | :row | :sum | :binary |
; |--------------------------------------------------------------+-------+-----------------|
; | (1) | 1 | 1 |
@rxacevedo
rxacevedo / trampoline.clj
Last active August 29, 2015 14:03
Using trampoline to process a long sequence of mutually recursive function calls.
(letfn [(m-even [v]
(if (zero? v) v
(fn [] (m-odd (dec v)))))
(m-odd [v]
(if (zero? v) v
(fn [] (m-even (dec v)))))]
(trampoline m-even 100000))
;; => 0
(letfn [(m-even [v]
(if (zero? v) v