Skip to content

Instantly share code, notes, and snippets.

View scratch.clj
(let [addr ( "/tmp/222")
sc (doto (java.nio.channels.ServerSocketChannel/open
(.bind addr))]
(while true
(let [c (.accept sc)]
(let [in (java.nio.channels.Channels/newInputStream c)
out (java.nio.channels.Channels/newOutputStream c)]
(binding [*in* (clojure.lang.LineNumberingPushbackReader.
( in))
View pprint_middleware.clj
(ns dev.nocommit.pprint-middleware
(:require [honeysql.core :as hsql]
[sql-honey.core :as sqlhoney]
[metabase.models.database :refer [Database]]
[clojure.string :as str])
(:import org.apache.openjpa.lib.jdbc.SQLFormatter))
(let [formatter (SQLFormatter.)]
(.prettyPrint formatter "select * from orders")))
View repl.clj
(require '[clojure.string :as str])
(require '[clojure.core.server :as server])
#_(peek (str/split (str *ns*) #"\.")) ;> is just displayed as quux>
;; keep prompts shorter
:prompt (fn [] (printf "%s=> " (peek (str/split (str *ns*) #"\."))))
;; allow easily ending this sub repl watching for :repl/quit
:read server/repl-read)
;; fantastic reveal
View heap-reducing.clj
;; use a heap to keep only the max N (here 10) items in memory at a time. imagine reducing over a
;; quite large result set. don't want to realize it all, sort, and then take N. Just keep discarding
;; compared to our heap's min item.
(let [threshold 10
cmp (reify java.util.Comparator
(compare [_ x y]
;; score is second so want to compare [score item]
(compare (vec (reverse x)) (vec (reverse y)))))
xf (comp (map (fn [{:keys [name]}] [name (count name)]))
View repro-shadow.cljs
;; shadow-cljs.edn: {:dependencies [[org.clojure/test.check "1.1.0"]]}
;; npm i shadow-cljs
;; cider-jack-in-cljs
;; shadow
;; node-repl
foo> (require '[clojure.spec.test.alpha :as spec])
foo> (spec/check)
View register.el
;; ‘C-x r s <register-key>’ save to register
;; 'C-c C-j x <register-key' to send to repl
(defun cider-insert-register-contents (register)
(interactive (list (register-read-with-preview "From register")))
(let ((form (get-register register)))
;; could put form into a buffer and check if its parens are
;; balanced
(if form
(cider-insert-in-repl form (not cider-invert-insert-eval-p))
(user-error "No saved form in register"))))
View defcustoms.el
(let (defcustoms)
(dolist (file (directory-files "/Users/dan/projects/dev/cider" t "\.el"))
(unless (string-match-p "dir-locals" file)
(with-current-buffer (find-file file)
(goto-char (point-min))
(while (re-search-forward "defcustom" nil t)
(let ((custom (substring-no-properties (progn (paredit-forward 1)
(thing-at-point 'sexp))))
(docstring (substring-no-properties (progn (paredit-forward 2)
(or (thing-at-point 'sexp)
View into-str.clj
;; thanks ghadi from #clojure.beginners
(defn into-str
"reduce coll into a String, given a transducer"
[xf coll]
(transduce (comp xf (map str))
([] (StringBuilder.))
([^StringBuilder sb] (.toString sb))
([^StringBuilder sb s] (.append sb ^String s)))
View filewalker.clj
(reify FileVisitor
(visitFile [_ p attrs]
(let [f (.relativize src p)
last-mod (Files/getLastModifiedTime p (make-array LinkOption 0))]
(when *verbose* (println (str (.toString src) "/" (.toString f))))
(with-open [is (Files/newInputStream p (make-array OpenOption 0))]
(copy! (.toString f) is (.resolve dest (.toString f)) last-mod)))
(preVisitDirectory [_ p attrs]
(Files/createDirectories (.resolve dest (.toString (.relativize src p)))
dpsutton / future-utils.clj
Last active May 17, 2020
ghadi's completeable future helpers from slack
View future-utils.clj
(defn ->Function
(reify java.util.function.Function
(apply [_ obj] (f obj))))
(defn future-map
[cf f]
(.thenApply cf (->Function f)))
(defn future->ch
(future->ch cf (async/chan 1) true))