Skip to content

Instantly share code, notes, and snippets.

View favila's full-sized avatar

Francis Avila favila

View GitHub Profile
@favila
favila / tx-pipeline-inorder.clj
Created April 4, 2017 00:17
Transact transactions in core.async channel `from-ch` against datomic connection `conn` with a pipeline depth of `n`.
(defn tx-pipeline-inorder
"Transact transactions in core.async channel `from-ch` against datomic connection `conn` with a pipeline depth of `n`.
Returns a map with keys `:stop!` and `:result`. `:stop!` is a no-arg function you can call to immediately
cease sending transactions (already sent transactions cannot be stopped). `:result` is a promise channel
returning a variant of the result of the tx-pipelining after from-ch is closed and drained, or
:stop! is called and all send transaction futures are deref-ed, or a transaction produces an exception.
The variant returned from the :result channel may be one of:
@favila
favila / build.clj
Last active March 29, 2017 08:57
See how js output adapts to boolean hinting and tidy vs messy predicates. JS code emitted for predicates is much better if you explicitly test for nils and thus convert to bools.
;; Using clojurescript master 1.9.512 uberjar
;; Commit 52ff7a2bdcaacf840f7aae72f4be4575297f7db1
(require 'cljs.build.api)
(cljs.build.api/build "src" {:optimizations :advanced
:pretty-print true
:pseudo-names true
:output-to "out/main.js"})
@favila
favila / print_bits.js
Last active March 15, 2017 22:33
Print binary representation of js numbers cast to ints or longs
function assertSafe(n) {
if (!Number.isSafeInteger(n)) {
throw new RangeError("Not a double-safe integer: " + n);
}
}
function intBits(n) {
assertSafe(n);
var low32 = (n >>> 0).toString(2);
low32 = "0".repeat(32 - low32.length) + low32;
@favila
favila / tx-ids.clj
Last active May 24, 2019 00:46
Lazy tx-ids for datomic that works outside query and does not rely on a connection or the log
(require '[datomic.api :as d])
(def conn-uri "datomic:dev://localhost:4334/my-db")
;; This is normally how you would get a list of tx-ids outside a query.
;; However, there is some concern that this is not lazy. (I am pretty sure,
;; but not certain, that reading the tx-log is lazy.)
(-> conn-uri d/connect d/log (d/tx-range nil nil)
(->> (map :t) (take 10)))
@favila
favila / partition-key-by.cljc
Last active March 1, 2017 21:30
partition-key-by: Clojure transducer which emits the value of `(f x)` every time it changes. Useful when you want to use distinct on a collection you know is already sorted, or group-by but only care about the keys.
(defn partition-key-by
"Return a transducer which emits the value of `(f x)` every time it changes.
This is the same as the following, except far more efficient because it will
not retain large intermediate collections:
(comp
(map f)
(partition-all)
(map first))
@favila
favila / entity-pull.clj
Created February 8, 2017 18:19
entity-pull: a datomic.api/pull that returns values the way datomic.api/entity would, with sets and ident keywords
(require '[datomic.api :as d])
(defn entity-pull
"Like pull, but returns values consistent with d/entity, i.e.,
entities with :db/ident are represented as keywords and sets are
used instead of vectors."
[db pat eid]
(->> (d/pull db pat eid)
(clojure.walk/prewalk
(fn [x]
;; same thing using reduction over d/datoms.
;; Objective is to avoid realizing too much in memory
;; May still be slow, but won't OOM
(defn orgs->max-fks [db]
(reduce (fn [o->fk [content-eid _ content-fk]]
(let [org-id (-> (d/entity db content-eid)
:content/collection
:collection/organization
:db/id)]

Keybase proof

I hereby claim:

  • I am favila on github.
  • I am favila (https://keybase.io/favila) on keybase.
  • I have a public key whose fingerprint is 594B BA31 C16A BA70 3AF4 AFB5 173E 02EE 2F85 597C

To claim this, I am signing this object:

package mytransit;
import com.cognitect.transit.TransitFactory;
import com.cognitect.transit.WriteHandler;
import com.cognitect.transit.Writer;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
@favila
favila / core-async-promise-all.cljs
Created July 28, 2016 22:38
Given a collection of takeable items, return a promise channel which will contain a vector of the next available value taken from every item, preserving order if it exists in the original collection. If any takeable is closed the promise will be closed. Runs as synchronously as possible using poll! and offer!, only becoming async if one of the t…
(defn promise-all
"Given a collection of takeable items, return a promise channel which will
contain a vector of the next available value taken from every item, preserving
order if it exists in the original collection. If any takeable is closed
the promise will be closed.
Runs as synchronously as possible using poll! and offer!, only becoming async
if one of the takeable items is not immediately takeable.
Example: