Project

fzip

0.0
No commit activity in last 3 years
No release in over 3 years
Functional zipper class
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 10.1
~> 2.14
 Project Readme

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.