Live data from Hacker News

Clojure Zippers (2021)

grishaev.me

1–9 of 9 posts

Re: Clojure Zippers (2021)

#4
I'm not sure if I get it. But I don't know Clojure syntax too well. Say I want to represent a slideshow state. I could do it with

    { 
      slides: List, 
      current_index: Int
    }
Or alternatively:

    {
      left_slides: List,
      current: Slide,
      right_slides: List
    }
Is it fair to call the latter a Zipper?

Re: Clojure Zippers (2021)

#5
post #4

I'm not sure if I get it. But I don't know Clojure syntax too well. Say I want to represent a slideshow state. I could do it with { slides: List , current_index: Int } Or alternatively: { left_slides: List , current: Slide, right_slides: List } Is it fair to call the latter a Zipper?

Yes if List is immutable and the interface for stepping through the slides ist designed accordingly

Re: Clojure Zippers (2021)

#6
post #4

I'm not sure if I get it. But I don't know Clojure syntax too well. Say I want to represent a slideshow state. I could do it with { slides: List , current_index: Int } Or alternatively: { left_slides: List , current: Slide, right_slides: List } Is it fair to call the latter a Zipper?

Yes, although it's usually defined as:

    {
      slides_shown: List,
      slides_left: List
    }
Where the head of slides_left is the current slide. Pretty much any recursive data structure can be derived into a zipper.

Re: Clojure Zippers (2021)

#7
I've used zippers a couple times.

Once for navigating a collection of deeply nested routes in a webapp, and once for navigating deeply nested xml to grab very particular data.

Both times it was pretty pleasant and nice to use.

I wouldn't reach for them in most normal situations cause they're more complicated to get right than simple looping (or `clojure.walk/prewalk`), but if you have large semi-predictable data structures, you can do cool stuff with zippers.