summaryrefslogtreecommitdiff
path: root/vendor/github.com/lann/ps/list.go
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-07-22 17:35:49 -0600
committermo khan <mo@mokhan.ca>2025-07-22 17:35:49 -0600
commit20ef0d92694465ac86b550df139e8366a0a2b4fa (patch)
tree3f14589e1ce6eb9306a3af31c3a1f9e1af5ed637 /vendor/github.com/lann/ps/list.go
parent44e0d272c040cdc53a98b9f1dc58ae7da67752e6 (diff)
feat: connect to spicedb
Diffstat (limited to 'vendor/github.com/lann/ps/list.go')
-rw-r--r--vendor/github.com/lann/ps/list.go93
1 files changed, 93 insertions, 0 deletions
diff --git a/vendor/github.com/lann/ps/list.go b/vendor/github.com/lann/ps/list.go
new file mode 100644
index 0000000..48a1ceb
--- /dev/null
+++ b/vendor/github.com/lann/ps/list.go
@@ -0,0 +1,93 @@
+package ps
+
+// List is a persistent list of possibly heterogenous values.
+type List interface {
+ // IsNil returns true if the list is empty
+ IsNil() bool
+
+ // Cons returns a new list with val as the head
+ Cons(val Any) List
+
+ // Head returns the first element of the list;
+ // panics if the list is empty
+ Head() Any
+
+ // Tail returns a list with all elements except the head;
+ // panics if the list is empty
+ Tail() List
+
+ // Size returns the list's length. This takes O(1) time.
+ Size() int
+
+ // ForEach executes a callback for each value in the list.
+ ForEach(f func(Any))
+
+ // Reverse returns a list whose elements are in the opposite order as
+ // the original list.
+ Reverse() List
+}
+
+// Immutable (i.e. persistent) list
+type list struct {
+ depth int // the number of nodes after, and including, this one
+ value Any
+ tail *list
+}
+
+// An empty list shared by all lists
+var nilList = &list{}
+
+// NewList returns a new, empty list. The result is a singly linked
+// list implementation. All lists share an empty tail, so allocating
+// empty lists is efficient in time and memory.
+func NewList() List {
+ return nilList
+}
+
+func (self *list) IsNil() bool {
+ return self == nilList
+}
+
+func (self *list) Size() int {
+ return self.depth
+}
+
+func (tail *list) Cons(val Any) List {
+ var xs list
+ xs.depth = tail.depth + 1
+ xs.value = val
+ xs.tail = tail
+ return &xs
+}
+
+func (self *list) Head() Any {
+ if self.IsNil() {
+ panic("Called Head() on an empty list")
+ }
+
+ return self.value
+}
+
+func (self *list) Tail() List {
+ if self.IsNil() {
+ panic("Called Tail() on an empty list")
+ }
+
+ return self.tail
+}
+
+// ForEach executes a callback for each value in the list
+func (self *list) ForEach(f func(Any)) {
+ if self.IsNil() {
+ return
+ }
+ f(self.Head())
+ self.Tail().ForEach(f)
+}
+
+// Reverse returns a list with elements in opposite order as this list
+func (self *list) Reverse() List {
+ reversed := NewList()
+ self.ForEach(func(v Any) { reversed = reversed.Cons(v) })
+ return reversed
+}