Skip to content

Instantly share code, notes, and snippets.

@Frozenlock
Frozenlock / test.cljs
Last active June 16, 2017 13:15 — forked from skrat/test.cljs
;; some speed tests of functions to access nested properties in JS objects.
(def test-data (->> (into {} (for [k1 (range 10)]
[k1 (into {} (for [k2 (range 10)]
[k2 (into {} (for [k3 (range 10)]
[k3 k3]))]))]))
(clj->js)))
(defn deep-merge
"Recursively merges maps. If vals are not maps, the last value wins."
[& vals]
(if (every? map? vals)
(apply merge-with deep-merge vals)
(last vals)))
(defn deep-merge-with
"Like merge-with, but merges maps recursively, applying the given fn
only when there's a non-map at a particular level."
(defn -inheritable*
[var]
(let [*i (doto (proxy [InheritableThreadLocal] []
;; This is where the magic happens.
;; childValue is evaluated in the parent thread where bindings should still valid.
(childValue [{:keys [bindings]}]
(let [new-bindings (merge bindings (get-thread-bindings))]
{:bindings new-bindings
:get-val (fn [] (with-bindings new-bindings
(deref var)))})))