Skip to content

Instantly share code, notes, and snippets.

@nathanmarz
nathanmarz / set-query.clj
Created January 16, 2024 22:44
foreign set query with Rama
;; in module code include this function:
(defn view-with-arg [view-fn arg]
(fn [v] (view-fn v arg)))
;; then foreign queries can be like this:
(foreign-select [(keypath :some-kid) (view (view-fn-with-arg contains? :some-swing))] swing-sessions-pstate)
@nathanmarz
nathanmarz / repl
Created July 31, 2019 19:13
Multiple namespace loading behavior
user=> (require 'test-ns)
Loading...
nil
user=> (require 'test-ns)
nil
user=> (require 'test_ns)
"Loading...
nil
user=> (require 'test_ns)
nil
### Keybase proof
I hereby claim:
* I am nathanmarz on github.
* I am nathanmarz (https://keybase.io/nathanmarz) on keybase.
* I have a public key ASC_ykwTrU3q__CBRbHWiRnZZf_Hg8ZSu9ye2a3nM4NU9Qo
To claim this, I am signing this object:
@nathanmarz
nathanmarz / ensure.clj
Created December 20, 2017 21:30
Remove empty subvalues
(let [ensure* (stay-then-continue (if-path empty? (terminal-val NONE)))]
(defdynamicnav ensure [& path]
(interleave (repeat (count path) ensure*) path)
))
(setval [:a (ensure :b :c)] NONE {:a {:b {:c 1}}})
;; => {}
(setval [:a (ensure :b :c)] NONE {:a {:b {:c 1} :d 2}})
;; => {:a {:d 2}}
@nathanmarz
nathanmarz / gist:9d8805d5e7d9c689cd629bd0551e84f1
Last active October 9, 2017 19:34
Specter vs "first" function
Benchmark code: https://github.com/nathanmarz/specter/blob/master/scripts/benchmarks.clj#L176
----------------------------------------------------------------------------------------------
Benchmark: first value of a size 10 vector (10000000 iterations)
Avg(ms) vs best Code
261.98 1.00 (select-any FIRST data)
277.11 1.06 (select-any ALL data)
337.43 1.29 (select-first ALL data)
561.89 2.14 (first data)
@nathanmarz
nathanmarz / benchmark.clj
Last active March 1, 2017 12:57
namespace qualify keys of a map benchmark
Specter 1.0
Clojure 1.8.0
Mac OSX 10.11.6
Benchmark: namespace qualify keys of a small map (1000000 iterations)
Avg(ms) vs best Code
243.69 1.00 (setval [MAP-KEYS NAMESPACE] (str *ns*) data)
432.11 1.77 (reduce-kv (fn [m k v] (assoc m (keyword (str *ns*) (name k)) v)) {} data)
682.69 2.80 (into {} (map (fn [[k v]] [(keyword (str *ns*) (name k)) v])) data)
@nathanmarz
nathanmarz / results-clojure-1.7.0.clj
Last active April 4, 2024 23:46
Specter 0.13.0 benchmark
Benchmark code at: https://github.com/nathanmarz/specter/blob/master/scripts/benchmarks.clj
Run against Clojure 1.7.0 and Java 1.7.0 on Mac OSX 10.11.6
Benchmark: get value in nested map (2500000 iterations)
Avg(ms) vs best Code
53.528 1.00 (-> data (get :a) (get :b) (get :c))
54.708 1.02 (-> data :a :b :c)
(defn fast-constantly [v]
(fn ([] v)
([a1] v)
([a1 a2] v)
([a1 a2 a3] v)
([a1 a2 a3 a4] v)
([a1 a2 a3 a4 a5] v)
([a1 a2 a3 a4 a5 a6] v)
([a1 a2 a3 a4 a5 a6 a7] v)
([a1 a2 a3 a4 a5 a6 a7 a8] v)
Benchmark: transform values of a small map (1000000 iterations)
Avg(ms) vs best Code
141.61 1.00 (map-vals-map-iterable data inc)
146.86 1.04 (reduce-kv (fn [m k v] (assoc m k (inc v))) {} data)
156.66 1.11 (reduce-kv (fn [m k v] (assoc m k (inc v))) (empty data) data)
161.20 1.14 (transform MAP-VALS inc data)
211.53 1.49 (persistent! (reduce-kv (fn [m k v] (assoc! m k (inc v))) (transient {}) data))
227.91 1.61 (map-vals-map-iterable-transient data inc)
332.09 2.35 (transform [ALL LAST] inc data)
(declarepath ConawWalker)
(providepath ConawWalker
(stay-then-continue
(cond-path map? MAP-VALS coll? ALL)
ConawWalker))
(transform [ALL MAP-VALS ConawWalker (pred :tempid)] :tempid data)