Skip to content

Instantly share code, notes, and snippets.

@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 / 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)))
(defn relate [& pairs]
(assert (even? (count pairs)) "relate requires an even number of arguments")
(->> pairs
(partition 2)
(map (fn [[k vs]] (map #(hash-map k %) vs)))
(apply map merge)))
(defn matches-specmap? [specmap m]
(reduce-kv
(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)))))