Instantly share code, notes, and snippets.

View on-the-client.cljs
(ns on-the-client
(:require [cognitect.transit :as transit]))
;; Let's imagine the server has written the transit payload into this tag:
;; <script type="application/json" id="my-app-data">...</script>
;; This will output the original clojure data
(->> (js/document.getElementById "my-app-data")
(transit/read (transit/reader :json)))
View core.cljc
(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)
(fn [action-name data]
View x-hazard-edition.clj
(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?)
View x-improved.clj
(defn using-xform--improved [chunks]
(transduce (comp (halt-when error?)
(x/by-key :a x/count))
(using-xform--improved chunks)
=> {0 3330, 1 3330, 2 3330} ;; Still works
View into.clj
([to xform from]
(if (instance? clojure.lang.IEditableCollection to)
(persistent! (transduce xform conj! (transient to) from))
(meta to))
(transduce xform conj to from)))
View original-with-bench.clj
(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%)
View with-an-error.clj
(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
View x-first-try.clj
(require '[net.cgrand.xforms :as x])
(defn using-xforms [chunks]
(into {}
(comp (halt-when error?)
(x/by-key :a x/count))
(using-xforms chunks)
View chunks.clj
(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))
View original.clj
(defn error? [x] (:error x))
(defn original [chunks]
(transduce (comp
(halt-when error?)
(map #(frequencies (map :a %))))
(completing (partial merge-with +))