Skip to content

Instantly share code, notes, and snippets.

@chpill
chpill / dabblet.css
Last active August 29, 2015 14:14 — forked from anonymous/dabblet.css
The first commented line is your dabblet’s title
/**
* The first commented line is your dabblet’s title
*/
background: #fff;
(defn error? [x] (:error x))
(defn original [chunks]
(transduce (comp
(halt-when error?)
(map #(frequencies (map :a %))))
(completing (partial merge-with +))
{}
chunks))
(defn make-chunks [chunk-count chunk-size]
(for [i (range chunk-count)]
(for [j (range chunk-size)]
{:a (mod j 3)})))
(def chunks (make-chunks 10 999))
(require '[net.cgrand.xforms :as x])
(defn using-xforms [chunks]
(into {}
(comp (halt-when error?)
cat
(x/by-key :a x/count))
chunks))
(using-xforms chunks)
(def mini-chunks-with-error
[[{:a 0} {:a 1} {:a 2}]
[{:a 0} {:a 1} {:a 2}]
{:error "Something terrible has happened!"}])
(original mini-chunks-with-error)
=> {:error "Something terrible has happened!"}
(using-xform mini-chunks-with-error)
=> Unhandled java.lang.ClassCastException
(original chunks)
=> {0 3330, 1 3330, 2 3330} ;; as expected!
;; Note that the standard deviation is huge here
(criterium/quick-bench (original chunks))
"
Evaluation count : 294 in 6 samples of 49 calls.
Execution time mean : 10.778696 ms
Execution time std-deviation : 8.818884 ms
Execution time lower quantile : 1.680341 ms ( 2.5%)
([to xform from]
(if (instance? clojure.lang.IEditableCollection to)
(with-meta
(persistent! (transduce xform conj! (transient to) from))
(meta to))
(transduce xform conj to from)))
(defn using-xform--improved [chunks]
(transduce (comp (halt-when error?)
cat
(x/by-key :a x/count))
conj
{}
chunks))
(using-xform--improved chunks)
=> {0 3330, 1 3330, 2 3330} ;; Still works
(defn single-return-transducer
"Dummy transducer that simply returns the value given to it,
ignoring any accumulated value"
([] :initial-value-to-be-ignored)
([final-value] final-value)
([initial-value v] v))
(defn using-xform--hazard-edition [chunks]
(transduce (comp (halt-when error?)
cat
@chpill
chpill / core.cljc
Last active May 2, 2017 21:29
Pseudo redux with rum bindings in clojurescript
(ns remux.core)
(defn create-store [handlers initial-state]
(let [state (atom initial-state)
dispatching? (atom false)]
{::state state
::get-state (fn get-state [] @state)
::get-raw-state #(state)
::dispatch
(fn [action-name data]