Skip to content

Instantly share code, notes, and snippets.

(defmacro with-timeout [seconds & body]
`(let [f# (future ~@body)]
(try
(.get f# ~seconds java.util.concurrent.TimeUnit/SECONDS)
(catch java.util.concurrent.TimeoutException e#
(do (future-cancel f#) nil)))))
@comnik
comnik / spit-url.cljs
Created January 21, 2018 14:15
Helper to export lots of data from a CLJS web-app, as a downloadable file.
(defn spit-url [content]
(let [blob (js/Blob. (js/Array. content) {:type "text/plain;charset=utf-8"})]
(.createObjectURL js/URL blob)))
@comnik
comnik / relational-thingies.js
Last active May 4, 2018 20:25
Clojure core.set thingies in JS
const selectKeys = (m, keys) => {
return keys.reduce((acc, k) => {
acc[k] = m[k];
return acc;
}, {});
}
const invertMap = (m) =>
Object.keys(m).reduce((acc, k) => {
acc[m[k]] = k;
@comnik
comnik / diff-d3.js
Last active June 22, 2018 07:32
A hacky re-write of the d3/data join operation to work directly with output from Differential Dataflow.
//
// Usage
//
// where before you might've had:
// const selection = parent.selectAll('li').data(data, keyFn)
//
// you now have:
const selection = diff(parent.selectAll('li'), data, keyFn)
@comnik
comnik / policing.clj
Created June 25, 2018 22:22
Applying access policies to Datomic queries.
(require '[datomic.api :as d])
(def uri "datomic:mem://policing")
(d/create-database uri)
(def conn (d/connect uri))
;; We are managing classified documents.
(def schema
[{:db/ident :level/rank
@comnik
comnik / memoize-mapping.clj
Created July 21, 2018 13:09
Deterministic mapping w/o carrying state, via memoize.
(def encode-symbol (memoize (fn [sym] (clojure.lang.RT/nextID))))
@comnik
comnik / morgana-relations.clj
Created November 14, 2018 00:03
Generating data relationships with datamorgana
(s/def :db/id (s/with-gen number? #(gen/fmap (fn [_] (clojure.lang.RT/nextID)) (gen/int))))
(s/def :entity/name (s/and string? #(< 3 (count %) 10)))
(s/def :entity/shareholder :db/id)
(s/def :person/id (s/with-gen string? #(s/gen #{"Alice" "Bob" "Eve" "Mabel" "Dipper"})))
(s/def :person/alias :db/id)
(s/def :company/name (s/with-gen string? #(s/gen #{"Alice" "Bob" "Eve" "Mabel" "Dipper"})))
(s/def :company/subsidiary :db/id)
@comnik
comnik / btset.cljc
Created November 16, 2018 20:01
Unary Leapfrog on datascript.btset
;; the BTset namespace needs has to be extended by the
;; following helper
(defn iter-seek
"Returns iterator for all X where key-from <= X."
[^Iter iter key-from]
(let [path (-seek (.-set iter) key-from)]
(when-not (neg? path)
(Iter. (.-set iter) path (.-right iter) (keys-for (.-set iter) path) (path-get path 0)))))
@comnik
comnik / shipping.rs
Last active April 16, 2019 06:52
The shipping puzzle (https://kevinlynagh.com/notes/shipping-puzzle/) in Differential Dataflow
#[macro_use]
extern crate abomonation_derive;
extern crate abomonation;
extern crate timely;
extern crate differential_dataflow;
use std::fs::File;
use std::io::{BufRead,BufReader};
use timely::dataflow::*;
<script src="https://github.com/tonsky/datascript/releases/download/0.18.2/datascript-0.18.2.min.js"></script>
<script type="text/javascript">
const d = datascript;
const schema = {
"db/ident": {":db/unique": ":db.unique/identity"},
"operator/name": {":db/cardinality": ":db.cardinality/one"},
"operator/address": {":db/unique": ":db.unique/identity"},