Skip to content

Instantly share code, notes, and snippets.

View stuarthalloway's full-sized avatar

Stuart Halloway stuarthalloway

View GitHub Profile
@stuarthalloway
stuarthalloway / missing_keys_specs.clj
Created October 14, 2017 11:44
I think it would be a mistake to introduce temporal coupling to prevent typos.
;; I think it would be a mistake to introduce temporal coupling to prevent typos.
;; The example program below lets you identify "missing" keys specs at
;; the time and place of your choosing, and then handle them as you
;; deem appropriate, without imposing those decisions on other
;; users of spec.
(require '[clojure.spec.alpha :as s]
'[clojure.set :as set])
@stuarthalloway
stuarthalloway / Datomic News Updates
Created June 18, 2012 14:53
Datomic update examples against a social news database
;; Datomic example code
;; demonstrates various update scenarios, using a news database
;; that contains stories, users, and upvotes
;; grab an in memory database
(use '[datomic.api :only (q db) :as d])
(def uri "datomic:mem://foo")
(d/create-database uri)
(def conn (d/connect uri))
@stuarthalloway
stuarthalloway / gist:2002582
Created March 8, 2012 18:39
Datomic extent query
;; Datomic example code
;;
;; The extent of entity ?x is all datoms that are about ?x.
;; Drop this into your rules.
;;
;; Demonstrates
;;
;; 1. recursive query (extent calls itself)
;; 2. disjunction (different extent bodies are ORed)
;; 3. component attributes (e.g. your arm is a component, your brother isn't)
@stuarthalloway
stuarthalloway / gist:2645453
Created May 9, 2012 15:22
Datomic queries against Clojure collections
;; Datomic example code
(use '[datomic.api :only (db q) :as d])
;; ?answer binds a scalar
(q '[:find ?answer :in ?answer]
42)
;; of course you can bind more than one of anything
(q '[:find ?last ?first :in ?last ?first]
"Doe" "John")
@stuarthalloway
stuarthalloway / clojure_spec_missing_piece.clj
Created March 17, 2017 01:11
Clojure spec's missing piece
;; clojure.spec's missing piece, work in progress
;; this is only halfway done, somebody else will need to do the other 95%
(require
'[clojure.spec :as s]
'[clojure.spec.test :as test])
(defn naive-english-explain
"Copy and paste this into your app. Figure out what it does by
trying it in production."
@stuarthalloway
stuarthalloway / Nil Finder
Created January 15, 2015 20:51
Finding those nested nils
(ns user)
(def app
"Intenal Helper"
(fnil conj []))
(defprotocol PathSeq
(path-seq* [form path] "Helper for path-seq"))
(extend-protocol PathSeq
(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)))
;; try this form-by-form at a REPL
(require '[clojure.spec.alpha :as s])
;; create an inline DSL to describe the FizzBuzz world
(defmacro divides-by
[nm n]
`(s/def ~nm (s/and pos-int? #(zero? (mod % ~n)))))
;; specify FizzBuzz
(divides-by ::fizz 3)
;; 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))))