Skip to content

Instantly share code, notes, and snippets.

@PetrGlad
Last active December 6, 2016 11:28
Show Gist options
  • Save PetrGlad/d2afedb2ba24054c7d99811507d7bfd0 to your computer and use it in GitHub Desktop.
Save PetrGlad/d2afedb2ba24054c7d99811507d7bfd0 to your computer and use it in GitHub Desktop.
;; A version of zipper that allows mixing maps and vectors
;; Note that it traverses map entries too
(require '[clojure.zip :as z])
(defn map-vec-zipper [m]
(z/zipper
(fn [x] (or (map? x) (sequential? x)))
seq
(fn [p xs]
(if (isa? (type p) clojure.lang.MapEntry)
(into [] xs)
(into (empty p) xs)))
m))
(-> (map-vec-zipper [{4 [22 21] 1 [3]}])
z/down
(z/edit assoc :e 99)
z/down
;; Note that the map does not guarantee particular ordering.
z/down ;; Getting into map entry.
z/next
(z/edit conj 77)
z/root)
;;=> [{1 [21 22 77], 3 [4], :e 99}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment