Skip to content

Commit 71bc79f

Browse files
authored
List Zipper (#86)
1 parent fdef496 commit 71bc79f

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

listzipper/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
### List Zipper
2+
3+
In functional programming, there's a concept called the List Zipper, which is a really elegant way of navigating and modifying lists. Imagine you have a list, but instead of just thinking of it as a flat sequence, you pick a point in that list to "focus" on. You divide the list into two parts: everything before that point (which we typically store in reverse order for efficiency), and everything from that point onward. The focus allows you to move left or right, almost like a cursor in a text editor, and perform edits in a very localized way.
4+
5+
## Task
6+
7+
Your task is to implement a generic List Zipper in Go that supports creation from a standard list or empty state, navigation by moving the focus left or right, inspection of the current focused value, modification through insertion or deletion near the focus, and conversion back to a regular list. Importantly, all operations must respect immutability—meaning any modifying function must return a new instance of the zipper without altering the original. The structure should have two fields: left (a reversed slice of elements before the focus) and right (a slice of elements at and after the focus). For example, moving the focus left would shift the last element of left to the front of right, and moving right would do the opposite. If right is empty, the focus is at the end; if left is empty and right is not, it’s at the beginning.
8+
9+
Note that it must contain fields to represent the elements before the focus (`left`, reversed) and the elements at/after the focus (`right`).
10+
11+
12+
For example, say you start with the list [1, 2, 3, 4]. If you move right once, your focus shifts from 1 to 2. Then maybe you insert 99, so the list becomes [1, 99, 2, 3, 4]. The original list and zipper stay untouched—you get a new zipper each time. That’s the whole idea: local, safe changes without side effects.
13+
14+
### Run tests with benchmarks
15+
16+
```
17+
go test -bench .
18+
```

listzipper/listzipper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package template
2+
3+
// Run func
4+
func Run() {}

listzipper/listzipper_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package template
2+
3+
import "testing"
4+
5+
func TestRun(t *testing.T) {}
6+
7+
func BenchmarkRun(b *testing.B) {
8+
for i := 0; i < b.N; i++ {
9+
Run()
10+
}
11+
}

0 commit comments

Comments
 (0)