Skip to content

Instantly share code, notes, and snippets.

@chpill
chpill / on-the-client.cljs
Last active May 21, 2017 14:17
An example of some server code to encode clojure data as transit, and some client code to decode the transit into clojurescript datastructures.
(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")
.-innerHTML
(transit/read (transit/reader :json)))
@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]
(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
(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
([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)))
(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%)
(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
(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)
(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))
(defn error? [x] (:error x))
(defn original [chunks]
(transduce (comp
(halt-when error?)
(map #(frequencies (map :a %))))
(completing (partial merge-with +))
{}
chunks))