Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View stuarthalloway's full-sized avatar

Stuart Halloway stuarthalloway

View GitHub Profile
(defn pull-many
"Pull pattern from each entity in eids in a single eager query.
Returns a seqable of values sorted by:
sort-k if sort-k non nil
eids order if no sort-k specified
unsorted if sort-k is nil"
([db pattern eids]
(let [e->idx (into {} (map-indexed (fn [idx e] [e idx]) eids))]
(pull-many db pattern #(-> % :db/id e->idx) eids)))
;; good repro #0: problem statement
;; in n takes (tricks: translate domain words into Datomic words, be precise.)
;; 0. "Two datoms in transaction conflict" move an entity id to a reference entity take
;; 1. "Retracting an entity and adding an entity"
;; ...
;; 5. "Retracting a unique identity datom and asserting the same identity on another datom causes
;; "2 datoms in transaction conflict"
;; good repro #1: depend only on Datomic API
(defn squuid-created-at
"Pull the date back out of a squuid. This will be nonsense if the UUID
passed in was not a squuid!"
[^java.util.UUID uuid]
(let [secs (bit-shift-right (.getMostSignificantBits uuid) 32)]
(java.util.Date. (* secs 1000))))
;; see also https://twitter.com/thing_umbrella/status/1111427898487898113
(require '[clojure.spec.alpha :as s])
;; spec that ensures the keys in renames match the keys in map
(s/def ::rename-keys-args
(s/and (s/cat :map map? :renames map?)
(fn [{:keys [map renames]}]
(every? map (keys renames)))))
;; ok
@stuarthalloway
stuarthalloway / example.clj
Created February 8, 2019 14:01
What Clojure errors look like
(let [a 1 b])
Syntax error macroexpanding clojure.core/let at (REPL:5:1).
[a 1 b] - failed: even-number-of-forms? at: [:bindings] spec: :clojure.core.specs.alpha/bindings
@stuarthalloway
stuarthalloway / specs_over_tests.clj
Created October 13, 2018 10:46
Capture domain knowledge once in specs, instead of burying it in tests
;; compare to https://gist.github.com/lensgolda/13e549476fb65d3b8c2d71ca59b15937#file-test-clj
(require '[clojure.spec.alpha :as s])
;; general domain knowledge, not buried in a test
(s/def ::info (s/keys :req-un [::name]))
(s/def ::items (s/coll-of pos-int?))
(s/def ::active boolean?)
;; knowledge specific to this test data
(ns foo)
(defn hello [x] (str "Hi " x))
(ns bar)
(println (foo/hello "there"))
(remove-ns 'foo)
(println (resolve 'foo/hello))
@stuarthalloway
stuarthalloway / clj_2373_examples.clj
Last active August 22, 2018 14:50
Intended for form-at-a-time at the REPL
(require
'[clojure.main :as main]
'[clojure.pprint :as pp])
(def strings
"Some strings that should fail somewhere in read/compile"
[":::5"
"(let [x])"
"(cond 1)"
"defmulti 5 class)"
(->> [(cycle [:fizz :_ :_])
(cycle [:buzz :_ :_ :_ :_])]
(apply map vector)
(take 25))
@stuarthalloway
stuarthalloway / clj_2373_take_1.clj
Created July 9, 2018 17:14
Clojure repl-caught and pst printing after applying https://dev.clojure.org/jira/browse/CLJ-2373
(comment "interop runtime exception repl-caught")
(/ 1 0)
ArithmeticException Divide by zero clojure.lang.Numbers.divide (Numbers.java:163)
(comment "interop runtime exception pst")
(pst)
ArithmeticException Divide by zero
clojure.lang.Numbers.divide (Numbers.java:163)
clojure.lang.Numbers.divide (Numbers.java:3833)
;; elided