9// LazySlice is a thread-safe lazy-loaded slice.
10type LazySlice[K any] struct {
15// NewLazySlice creates a new slice and runs the [load] function in a goroutine
17func NewLazySlice[K any](load func() []K) *LazySlice[K] {
25// Seq returns an iterator that yields elements from the slice.
26func (s *LazySlice[K]) Seq() iter.Seq[K] {
28 return func(yield func(K) bool) {
29 for _, v := range s.inner {
37// Slice is a thread-safe slice implementation that provides concurrent access.
38type Slice[T any] struct {
43// NewSlice creates a new thread-safe slice.
44func NewSlice[T any]() *Slice[T] {
50// NewSliceFrom creates a new thread-safe slice from an existing slice.
51func NewSliceFrom[T any](s []T) *Slice[T] {
52 inner := make([]T, len(s))
59// Append adds an element to the end of the slice.
60func (s *Slice[T]) Append(items ...T) {
63 s.inner = append(s.inner, items...)
66// Prepend adds an element to the beginning of the slice.
67func (s *Slice[T]) Prepend(item T) {
70 s.inner = append([]T{item}, s.inner...)
73// Delete removes the element at the specified index.
74func (s *Slice[T]) Delete(index int) bool {
77 if index < 0 || index >= len(s.inner) {
80 s.inner = slices.Delete(s.inner, index, index+1)
84// Get returns the element at the specified index.
85func (s *Slice[T]) Get(index int) (T, bool) {
89 if index < 0 || index >= len(s.inner) {
92 return s.inner[index], true
95// Set updates the element at the specified index.
96func (s *Slice[T]) Set(index int, item T) bool {
99 if index < 0 || index >= len(s.inner) {
102 s.inner[index] = item
106// Len returns the number of elements in the slice.
107func (s *Slice[T]) Len() int {
113// SetSlice replaces the entire slice with a new one.
114func (s *Slice[T]) SetSlice(items []T) {
117 s.inner = make([]T, len(items))
121// Seq returns an iterator that yields elements from the slice.
122func (s *Slice[T]) Seq() iter.Seq[T] {
123 return func(yield func(T) bool) {
124 for _, v := range s.Seq2() {
132// Seq2 returns an iterator that yields index-value pairs from the slice.
133func (s *Slice[T]) Seq2() iter.Seq2[int, T] {
135 items := make([]T, len(s.inner))
138 return func(yield func(int, T) bool) {
139 for i, v := range items {
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list