Zippers: Making Functional "Updates" Efficient (2010)
1–10 of 16 posts
Re: Zippers: Making Functional "Updates" Efficient (2010)
#2Re: Zippers: Making Functional "Updates" Efficient (2010)
#3Re: Zippers: Making Functional "Updates" Efficient (2010)
#4Re: Zippers: Making Functional "Updates" Efficient (2010)
#5I can see how this is useful if you’re repeatedly updating the same part of a tree. I can’t quite see how to use this approach for random edits. Seems like you’re back at recreating all the nodes back up to the root every time?
Re: Zippers: Making Functional "Updates" Efficient (2010)
#6https://github.com/xdavidliu/fun-problems/blob/main/zipper-t...
Re: Zippers: Making Functional "Updates" Efficient (2010)
#7Re: Zippers: Making Functional "Updates" Efficient (2010)
#8A concrete example is for managing the active item in a list. Instead of storing the active item as an index into the vector like this:
struct List {
items: Vec,
active: usize,
}
...which two the glaring impossible states. The vector can be empty, or the index can be outside the vector. Each time the active item is desired, we must check the index against the current state of the list.Instead, we can use the zipper concept so we always have a concrete active item:
struct List {
prev: Vec,
active: T,
next: Vec,
}
Switching to a different active item requires some logic internal to the data structure, but accessing the active item always results in a concrete instance with no additional checks required.[0]: https://sporto.github.io/elm-patterns/basic/impossible-state...
Re: Zippers: Making Functional "Updates" Efficient (2010)
#9I've used the zipper concept with lists for making impossible states impossible [0] in the context of Rust programs. The rich enum type in Rust creates opportunities to avoid bugs by baking small state machines into the code everywhere, like loading data in the linked example. A concrete example is for managing the active item in a list. Instead of storing the active item as an index into the vector like this: struct…
It's the API that makes something impossible to misuse, and they could offer the same API like List.create(x: T, xs: T[]), but the first one is simpler.
Re: Zippers: Making Functional "Updates" Efficient (2010)
#10I've used the zipper concept with lists for making impossible states impossible [0] in the context of Rust programs. The rich enum type in Rust creates opportunities to avoid bugs by baking small state machines into the code everywhere, like loading data in the linked example. A concrete example is for managing the active item in a list. Instead of storing the active item as an index into the vector like this: struct…