Skip to content

Instantly share code, notes, and snippets.

View favila's full-sized avatar

Francis Avila favila

View GitHub Profile
@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]
@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 / 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 / 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 / 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 / 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 / str-test.cljs
Created April 6, 2017 20:32
Test different str implementations for cljs. Code used for this jsperf (which no longer runs due to DropBox): https://jsperf.com/cljs-core-str-possibilities
(ns str-test
(:import goog.string.StringBuffer))
(defn ^:export str-tostr
([x] (if (nil? x)
""
(.toString x)))
([x & ys]
(loop [sb (StringBuffer. (str-tostr x)) more ys]
(if more
@favila
favila / component-reachable.clj
Created April 17, 2017 15:21
Datomic rule to get all datoms for an entity and for all entities reachable via forward is-component attributes
'[[(component-reachable [?se] ?e ?a ?v ?tx)
[(identity ?se) ?e]
[?e ?a ?v ?tx]]
[(component-reachable [?se] ?e ?a ?v ?tx)
[?se ?sa ?sv ?tx]
[?sa :db/isComponent true]
[?sa :db/valueType ?type]
[?type :db/ident :db.type/ref]
(component-reachable ?sv ?e ?a ?v ?tx)]]
@favila
favila / drpcclient-example.clj
Created April 26, 2017 18:47
How to use DRPCClient in Java in storm 1.0.3 (at least)
;; Documentation on the storm site http://storm.apache.org/releases/1.0.3/Distributed-RPC.html
;; has an example that constructs DRPCClient in a way that is out-of-date.
;; And all the example code uses the LocalDRPCClient, so there's no docs on how to construct this object.
;; The problem is the new first parameter to the DRPCClient constructors. This should be the storm config map,
;; the same one topologies get and the one configured via storm.yaml. Ideally it is the identical map, but
;; if you are running the DRPCClient on a different process you may not have access to it.
;; Below is the minimum you need to get started on a not-very-customized storm cluster
@favila
favila / kafka-workbench.clj
Created May 2, 2017 18:20
code to fiddle with kafka behaviors
(ns kafka-workbench
(:require [franzy.clients.consumer.protocols :as c]
[franzy.clients.producer.protocols :as p]
[franzy.serialization.serializers :as serializers]
[franzy.serialization.deserializers :as deserializers]
[franzy.clients.consumer.client :as consumer]
[franzy.clients.producer.client :as producer]))
(def kafka-brokers [])