This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns user) | |
(def app | |
"Intenal Helper" | |
(fnil conj [])) | |
(defprotocol PathSeq | |
(path-seq* [form path] "Helper for path-seq")) | |
(extend-protocol PathSeq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
NewerOlder