Slice

package
v0.0.0-...-52bf310 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 20, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

package Slice provides a custom Slice type with chainable methods with API similar to that of the standard library's slices package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Slice

type Slice[T comparable] struct {
	Items []T
}

type Slice is alias for custom Generic slice type

func MapTo

func MapTo[T, U comparable](self Slice[T], fn func(T) U) Slice[U]

MapTo applies the given function to each element and returns a new Slice with the transformed elements of the specified type U. This is the type-safe version of [Map] method.

func New

func New[T comparable](items []T) Slice[T]

New return a new instance of Slice type

func (Slice[T]) All

func (self Slice[T]) All() iter.Seq2[int, T]

All returns an iterator over index-value pairs in the slice in the usual order.

func (*Slice[T]) Append

func (self *Slice[T]) Append(elems ...T)

Append appends a new single element to end of self

func (*Slice[T]) AppendSeq

func (self *Slice[T]) AppendSeq(seq iter.Seq[T])

AppendSeq appends the values from seq to the Slice

func (Slice[T]) At

func (self Slice[T]) At(n int) T

At returns the element at nth index of self. This is provided as a helper method for convenience, one can directly use Slice{}.Items[n]

func (Slice[T]) Backward

func (self Slice[T]) Backward() iter.Seq2[int, T]

Backward returns an iterator over index-value pairs in the Slice, traversing it backward with descending indices.

func (Slice[T]) Clone

func (self Slice[T]) Clone() []T

Items return the shallow copy of underlying slice

func (*Slice[T]) Concat

func (self *Slice[T]) Concat(elems []T)

Concat appends every element in elems to end of self

func (Slice[T]) Contains

func (self Slice[T]) Contains(v T) bool

Contains reports whether v is present in self.

func (Slice[T]) ContainsFunc

func (self Slice[T]) ContainsFunc(f func(T) bool) bool

ContainsFunc reports whether at least one element e of Slice satisfies f(e).

func (*Slice[T]) Delete

func (self *Slice[T]) Delete(i, j int)

Delete removes the elements self.Items[i:j] from self. Delete panics if j > self.Length() or self.Items[i:j] is not a valid slice of self. Delete is O(self.Length()-i), so if many items must be deleted, it is better to make a single call deleting them all together than to delete one at a time. Delete zeroes the elements self.Items[self.Length()-(j-i):self.Length()].

func (*Slice[T]) DeleteFunc

func (self *Slice[T]) DeleteFunc(del func(T) bool)

DeleteFunc removes any elements from self for which del returns true. DeleteFunc zeroes the elements between the new length and the original length.

func (Slice[T]) Equal

func (self Slice[T]) Equal(other Slice[T]) bool

Equal reports whether two slices are equal: the same length and all elements equal. If the lengths are different, Equal returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first unequal pair. Empty and nil slices are considered equal. Floating point NaNs are not considered equal.

func (Slice[T]) Index

func (self Slice[T]) Index(v T) int

Index returns the index of the first occurrence of v in self, or -1 if not present.

func (Slice[T]) IndexFunc

func (self Slice[T]) IndexFunc(f func(T) bool) int

IndexFunc returns the first index i satisfying f(self.At(i)), or -1 if none do.

func (*Slice[T]) Insert

func (self *Slice[T]) Insert(i int, v ...T)

Insert inserts the values v... into self at index i. The elements at self.Items[i:] are shifted up to make room. In the modified Slice, self.At(i) == v[0], and, if i < self.Length(), self.At(i+len(v)) == value originally at self.At(i). Insert panics if i > self.Length(). This function is O(self.Length() + len(v)).

func (Slice[T]) Length

func (self Slice[T]) Length() int

Length return the length of underlying slice

func (Slice[T]) Map

func (self Slice[T]) Map(fn func(T) any) Slice[any]

Map applies the given function to each element and returns a new Slice with the transformed elements of type U

func (*Slice[T]) Push

func (self *Slice[T]) Push(elem T)

Push inserts a single element to end of self

func (*Slice[T]) Reverse

func (self *Slice[T]) Reverse()

Reverse reverses the elements of the slice in place.

func (Slice[T]) Values

func (self Slice[T]) Values() iter.Seq[T]

Values returns an iterator that yields the slice elements in order.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL