Skip to content

Instantly share code, notes, and snippets.

View RutledgePaulV's full-sized avatar

Paul Rutledge RutledgePaulV

View GitHub Profile
; add this to your project.clj file to suppress compilation warnings
:injections
[(alter-var-root clojure.lang.RT/ERR
(constantly
(java.io.PrintWriter.
(org.apache.commons.io.output.NullWriter.))))]
(defn zip-entries [^ZipInputStream input]
(let [last (atom nil)]
(letfn [(inner [stream]
(lazy-seq
(let [previous @last]
(when previous (.closeEntry stream))
(if-some [entry (.getNextEntry stream)]
(cons (reset! last entry) (inner stream))
(.close stream)))))]
(inner input))))
(ns file-scraper.core
(:require
[skyscraper :refer :all]
[clojure.string :as strings]
[clj-http.client :as client]
[net.cgrand.enlive-html :refer :all]
[clojure.java.io :as io])
(:import (java.util UUID)
(java.io FileOutputStream)
(java.net URLDecoder)))
(defmacro defmethodset
[multifn dispatch-vals & fn-tail]
`(for [dispatch-val# ~dispatch-vals]
(defmethod ~multifn dispatch-val# ~@fn-tail)))
; in util.clj
(ns project.util)
(defmacro create-test-class [name x]
(let [prefix (str name "-")]
`(do
(gen-class
:name ~(with-meta name `{Deprecated true})
:prefix ~(symbol prefix)
:methods [[~(with-meta 'getValue `{Deprecated true}) [] Integer]])
@RutledgePaulV
RutledgePaulV / nil-guarding.clj
Created November 23, 2016 17:29
A macro for wrapping execution of a single form such that the form is not evaluated and nil is returned when given any nil arguments
(defmacro ? [form]
(let [f (first form) args (rest form)]
`(let [_# (list ~@args)]
(when (every? some? _#)
(apply ~f _#)))))
@RutledgePaulV
RutledgePaulV / restful-routes-with-bidi.clj
Last active March 2, 2017 10:18
Never mess up a square bracket again!
(defn restful-routes
"Consumes vectors in the form of [node [child1 [innerChild1]] [child2 [innerChild2]] sibling1]
and produces a set of vectors that make up a bidi route structure. Path variables for ids are
automatically created and :$resource-name$-many keyword is used as a placeholder for the 'many'
endpoint and :$resource-name$-single keyword is used as a placeholder for the 'single' endpoint.
The intention is that after generating these routes, you can traverse the structure using clojure.walk
to swap placeholders for implementations."
[tree]
(letfn [(inner-generate-routes [tree]
@RutledgePaulV
RutledgePaulV / common-subtree-commonality.clj
Created October 10, 2016 04:58
A clojure implementation of common subtree commonality
(defn pairs [tree]
(letfn [(one-node [depth node]
(if (list? node)
(map (partial one-node (inc depth)) node)
(vector node depth)))]
(into [] (partition 2 (flatten (map (partial one-node 0) tree))))))
(defn foreach [f coll]
(dorun (map f coll)))
@RutledgePaulV
RutledgePaulV / subtrees.clj
Created October 10, 2016 04:52
Given a tree shaped list, decompose it into all subtrees.
(defn subtrees [tree]
(if (or (nil? tree) (empty? tree))
'()
(let [parent (first tree) children (take-while list? (rest tree))]
(if (not-empty children)
(concat
(map #(list parent %) children)
(mapcat subtrees children)
(subtrees (drop (inc (count children)) tree)))
(subtrees (next tree))))))
(defn aliasing [& keys-and-values]
"Creates a map from pairs of vector/value entries. Each entry in the vector is used as a key for the corresponding value."
(loop [result {} entries (into [] (partition 2 keys-and-values))]
(let [entry (first entries) keys (first entry) value (second entry)]
(if (empty? entries)
result
(recur (merge result (zipmap keys (repeatedly #(identity value)))) (rest entries))))))