Last active
November 1, 2015 14:12
-
-
Save hby/374aeabc076ee7780a97 to your computer and use it in GitHub Desktop.
For safely exploring potentially large structures in the repl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(declare explore-map) | |
(declare explore-vector) | |
(declare explore-seq) | |
(defn explore | |
"Safely explore possibly large map/vector structures in the repl. | |
morv - map or vector or seq | |
threshold - if count is under threshold, recursively explore | |
peekn - if over threshold, recursivley explore peekn entries | |
and summarize rest" | |
[morv threshold peekn] | |
(cond | |
(map? morv) (explore-map morv threshold peekn) | |
(vector? morv) (explore-vector morv threshold peekn) | |
(seq? morv) (explore-vector (into [] morv) threshold peekn) | |
:else (str morv))) | |
(declare big-map) | |
(defn explore-map | |
[m t p] | |
(if (< (count m) t) | |
(into {} (map (fn [[key val]] [key (explore val t p)]) m)) | |
(big-map m t p))) | |
(defn big-map | |
[m t p] | |
(let [peeked (take p m) | |
rest (drop p m) | |
more (str (count rest) " entries")] | |
(assoc (into {} | |
(map (fn [[key val]] [key (explore val t p)]) peeked)) | |
:more more))) | |
(declare big-vector) | |
(defn explore-vector | |
[v t p] | |
(if (< (count v) t) | |
(into [] (map (fn [e] (explore e t p)) v)) | |
(big-vector v t p))) | |
(defn big-vector | |
[v t p] | |
(let [peeked (take p v) | |
rest (drop p v) | |
more (str (count rest) " entries")] | |
(conj (mapv (fn [e] (explore e t p)) peeked) | |
more))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For example, I'm exploring my iTunes library.
gives
Hmm, the tracks map look like it only has 25 more keys.
Ok, Need to look at a few more tracks.
Now I have better idea of the structure and its variability.