form-zip

46 views
Skip to first unread message

George Jahad

unread,
Nov 2, 2011, 12:56:17 AM11/2/11
to Clojure
surely this one's been written before, but i needed it the other day
and couldn't find it.

form-zip returns a zipper from a clojure form.

user=> (require '[clojure.zip :as zip])
user=> (use 'form-zip.core)
user=> (-> '{1 2 3 4} form-zip zip/next zip/remove zip/root)
{3 4}

fz-node-seq returns a seq of the nodes.

user=> (fz-node-seq '{1 2 3 4})
({1 2, 3 4} [1 2] 1 2 [3 4] 3 4)

a one hour hack, probably full of edge cases i haven't thought of.

on clojars and github:
https://github.com/GeorgeJahad/form-zip

George Jahad

unread,
Nov 8, 2011, 10:21:35 PM11/8/11
to Clojure

Now that I think of it, why are seq-zip and vector-zip separate
functions? Why not a single function that handles both seq and
vectors, and sets and maps too?

What am I missing?

Alan Malloy

unread,
Nov 8, 2011, 11:04:34 PM11/8/11
to Clojure
They have a different make-node function, so that when you edit a
vector-zip you get vectors instead of something else. It's also easy
to imagine your "data units" are simple vectors, grouped together in
some kind of list structure. Then you would want the zipper to tell
you "hey, this node is a leaf" for vectors - thus seq-zip.

Alan Malloy

unread,
Nov 8, 2011, 11:06:32 PM11/8/11
to Clojure
(fz-node-seq x) is just (tree-seq coll? seq x) then, yeah? I could see
form-zip being useful for people who like zippers (personally I don't
have enough experience to be comfortable with them), but fz-node-seq
doesn't seem useful.

On Nov 1, 8:56 pm, George Jahad <cloj...@blackbirdsystems.net> wrote:

George Jahad

unread,
Nov 9, 2011, 9:35:08 AM11/9/11
to Clojure
I see your point. Still it seems odd that the default zipper
generating function isn't one that understands the "big 4" clojure
datastructures.

George Jahad

unread,
Nov 9, 2011, 9:40:30 AM11/9/11
to Clojure


> (fz-node-seq x) is just (tree-seq coll? seq x)

Good Point! I had never noticed the parallels between tree-seq and
zippers, but it's obvious when you point it out. Thanks for
mentioning that.

> (personally I don't have enough experience to be comfortable with
> them),

yeah, i haven't found a great use case for zippers yet either. but
lately I've been experimenting with using them to write more readable
code transforms. hence the need for form-zip.

Like clojure.walk, but with the ability to backtrack.


> fz-node-seq doesn't seem useful.

Ouch! Ok, here's one that is. As you astutely noted above,
fz-node-seq is functionally equivalent to (tree-seq coll? seq x), but
what's interesting about it is, it is implemented like so:
(map zip/node (fz-loc-seq form))

What's "useful" there is the call to fz-loc-seq which provides
a seq of every zipper loc in the form. Useful again for code
transforms, I think.

Thanks again for exercising my brain cells. I haven't forgotten that
beer I owe you.
Reply all
Reply to author
Forward
0 new messages