Fzip : Functional zippers for Ruby
To traverse and alter a hierarchical data structure in a functional way is best done using the "zipper" data structure as described originally by Huet.
Fzip::Zipper
is a straight up port of the Clojure zip/zip library.
The Zipper can work with arbitrary hierarchical objects (trees). To do that it requires an adapter that implements three methods: branch?(node)
, children(node)
, and create_node(node, children)
. An implementation for nested arrays is provided.
zipper = Fzip.array(
[
['x', '+', 'y'],
'=',
['a', '/', 'b']
]
)
p zipper.down.right.right.down.right.replace(:foo).root
# >> [["x", "+", "y"], "=", ["a", :foo, "b"]]
p zipper.down.append_child(:bar).root
# >> [["x", "+", "y", :bar], "=", ["a", "/", "b"]]
p zipper.down.insert_child(:bar).down.right.insert_right(:baz).up.insert_left(:quux).root
# >> [:quux, [:bar, "x", :baz, "+", "y"], "=", ["a", "/", "b"]]
Further reading
The Wikipedia page has more interesting links.