Suppose I need to walk over a list or tree to read (but not modify) the data. I can use either an iterator or a Zipper. Does Zipper
have any advantages aside from immutability in this case?
Asked
Active
Viewed 552 times
1 Answers
9
Do you need to backtrack or otherwise move around in the structure in a non-sequential order? Do you want to be able to traverse the structure multiple times without worrying about where you left the iteration? Do you want to avoid thinking about concurrent access or thread safety? Go with the zipper.
Do you know for a fact that you need the extra performance an iterator can provide in some situations? Are you working on a team that doesn't want to learn about a new abstraction? Go with the iterator.

Travis Brown
- 138,631
- 12
- 375
- 680
-
Thank you. Can you think about any use case that require non-sequential moving around a list ? – Michael Jun 01 '14 at 13:48
-
1Sure—suppose you need to perform some operation on every item in a list that also needs to know about each item's neighbors. You could keep track of that information yourself (in a fold or whatever), or you could let the zipper do the tracking for you. – Travis Brown Jun 01 '14 at 13:52
-
Suppose I need to generate a sequence `d | d[i] = a[i + 1] - a[i]` given a sequence `a`. Is it a good use case for the `zipper` ? – Michael Jun 01 '14 at 14:36
-
A zipper would work, but for something so simple just zipping the list against its tail would probably be more appropriate. Might be worth a new question if you want to get into details. – Travis Brown Jun 01 '14 at 14:52