Skip to content

Instantly share code, notes, and snippets.

@claj
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claj/c2f1bed4f425e33f7ecf to your computer and use it in GitHub Desktop.
Save claj/c2f1bed4f425e33f7ecf to your computer and use it in GitHub Desktop.
Steam-roller function
(steam-roller [:a {1 2 3 4} 1 1 :a :a])
-> ([:a {1 2, 3 4} 1 1 :a :a] :a {1 2, 3 4} 1 3 2 4 1 1 :a :a)
count: 11
;;identityHashCodes:
(map #(System/identityHashCode %) (steam-roller [:a {1 2 3 4} 1 1 :a :a]))
(1460028191
2005509007
1545655515
504432400
1778018115
1172818142
256714182
504432400
504432400
2005509007
2005509007)
count: 11
;;unique objects
(set (map #(System/identityHashCode %) (steam-roller [:a {1 2 3 4} 1 1 :a :a])))
#{2005509007
1172818142
1778018115
163464565
504432400
717359442
256714182}
count: 7
(steam-roller [:a :b {:c 2 3 4}])
([:a :b {:c 2, 3 4}] :a :b {:c 2, 3 4} :c 3 2 4)
(defn steam-roller [l]
"gives a representation of all object pointers in a
nested structure by both adding pointers to everything seqable
as well as its contents
it also takes out keys and vals of maps, as well as the whole map"
(let [l [l]]
(loop [l1 l l2 '()]
(cond
(sequential? (first l1)) (recur (concat (first l1) (rest l1)) (cons (first l1) l2))
(map? (first l1)) (recur (concat (keys (first l1)) (vals (first l1)) (rest l1)) (cons (first l1) l2))
(empty? l1) (reverse l2)
:else (recur (rest l1) (cons (first l1) l2))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment