Skip to content

Instantly share code, notes, and snippets.

@MageMasher
Created July 2, 2019 16:57

Revisions

  1. MageMasher created this gist Jul 2, 2019.
    99 changes: 99 additions & 0 deletions dynacode.repl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    (require '[datomic.client.api :as d]
    '[cognitect.transcriptor :as xr :refer (check!)])

    (def get-client
    "This function will return a local implementation of the client
    interface when run on a Datomic compute node. If you want to call
    locally, fill in the correct values in the map."
    (memoize #(d/client {:server-type :ion
    :region "us-east-1"
    :system "dynacode"
    :query-group "dynacode"
    :endpoint (str "http://entry." "dynacode" "." "us-east-1" ".datomic.net:8182")
    :proxy-port 8182})))

    (defn ensure-dataset
    "Ensure that a database named db-name exists, running setup-fn
    against a connection. Returns connection"
    [db-name]
    (let [client (get-client)]
    (d/create-database client {:db-name db-name})
    (let [conn (d/connect client {:db-name db-name})
    db (d/db conn)]
    {:conn conn
    :db db})))

    (def db-name "tuple-test2")

    (defn get-db-and-conn
    []
    (ensure-dataset db-name))

    (defn get-conn
    []
    (:conn (get-db-and-conn)))

    (defn get-db
    []
    (:db (get-db-and-conn)))

    (def conn (get-conn))

    (d/transact conn {:tx-data
    [{:db/ident :player/handle
    :db/valueType :db.type/string
    :db/cardinality :db.cardinality/one
    :db/unique :db.unique/identity}

    {:db/ident :player/location
    :db/valueType :db.type/tuple
    :db/tupleTypes [:db.type/long :db.type/long]
    :db/cardinality :db.cardinality/one}

    {:db/ident :player/skill
    :db/valueType :db.type/symbol
    :db/cardinality :db.cardinality/one
    :db/unique :db.unique/identity}]})


    (d/transact conn {:tx-data [{:player/handle "Argent Adept"
    :player/location [100 0]
    :player/skill 'clojure.core/sort}

    {:player/handle "Master Inc"
    :player/location [100 0]
    :player/skill 'clojure.core/inc}

    {:player/handle "Minor Dec"
    :player/location [100 0]
    :player/skill 'clojure.core/dec}

    {:player/handle "Vectorizer"
    :player/location [100 0]
    :player/skill 'clojure.core/vec}]})

    (defn play-skill [db player xs]
    (let [{skill :player/skill :or {skill 'clojure.core/identity}}
    (d/pull db '[:player/skill] [:player/handle player])]
    ((requiring-resolve skill) xs)))

    (def t-before-i-transacted-minor-dec 6)
    (def t-after-i-transacted-minor-dec 7)

    (play-skill (d/as-of (get-db) t-before-i-transacted-minor-dec) "Minor Dec" 1)
    (check! #{1})

    (play-skill (d/as-of (get-db) t-after-i-transacted-minor-dec) "Minor Dec" 1)
    (check! #{0})

    (play-skill (get-db) "Argent Adept" (shuffle (range 1 10)))
    (check! #{(range 1 10)})

    (play-skill (get-db) "Master Inc" 1)
    (check! #{2})

    (play-skill (get-db) "Vectorizer" (range 3))
    (check! #{[0 1 2]})

    (play-skill (get-db) "NotSo Adept" (shuffle (range 1 10)))
    (check! (comp not sorted?))