Skip to content

Instantly share code, notes, and snippets.

View RutledgePaulV's full-sized avatar

Paul Rutledge RutledgePaulV

View GitHub Profile
@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 / 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 / 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 _#)))))
; 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]])
(defmacro defmethodset
[multifn dispatch-vals & fn-tail]
`(for [dispatch-val# ~dispatch-vals]
(defmethod ~multifn dispatch-val# ~@fn-tail)))
(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)))
(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))))
; 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 attach-channel-cleanup [chan f]
(add-watch (.closed chan)
"channel-resource-cleanup"
(fn [_ _ old-state new-state]
(when (and (not old-state) new-state)
(f))))
chan)
(defmacro nor [& more]
(conj `~(partition 2 (interleave (repeat 'not) more)) 'and))