Documentation
¶
Overview ¶
Package slice provides a toolset to manipulate slices.
var s slice.Slice[int] s.Push(4, 5, 6) // [4 5 6] s.Unshift(1, 2, 3) // [1 2 3 4 5 6] s.Pop() // [1 2 3 4 5]
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
var s slice.Slice[int]
s.Push(4, 5, 6)
s.Unshift(1, 2, 3)
s.DeleteOne(0)
s.Pop()
fmt.Println(s)
}
Output: [2 3 4 5]
Index ¶
- type Slice
- func (s *Slice[T]) Cap() int
- func (s *Slice[T]) Clone() Slice[T]
- func (s *Slice[T]) Delete(index int, length int) (ok bool)
- func (s *Slice[T]) DeleteOne(index int) (ok bool)
- func (s *Slice[T]) Empty() bool
- func (s *Slice[T]) Filter(keep func(index int, val T) bool)
- func (s *Slice[T]) Get(index int) (_ T, ok bool)
- func (s *Slice[T]) Insert(index int, v ...T) (ok bool)
- func (s *Slice[T]) Len() int
- func (s *Slice[T]) Pop() (_ T, ok bool)
- func (s *Slice[T]) Push(v ...T)
- func (s *Slice[T]) Raw() []T
- func (s *Slice[T]) Replace(index int, v ...T) (ok bool)
- func (s *Slice[T]) Reverse()
- func (s *Slice[T]) Shift() (_ T, ok bool)
- func (s *Slice[T]) Shuffle(randIntN func(n int) int)
- func (s *Slice[T]) Sort(cmp func(a T, b T) int)
- func (s *Slice[T]) Unshift(v ...T)
Examples ¶
- Package
- Slice.Cap
- Slice.Clone
- Slice.Clone (Nil)
- Slice.Delete
- Slice.Delete (Last)
- Slice.DeleteOne
- Slice.Empty
- Slice.Filter
- Slice.Filter (Nil)
- Slice.Get
- Slice.Insert
- Slice.Insert (False)
- Slice.Len
- Slice.Pop
- Slice.Push
- Slice.Push (Nil)
- Slice.Raw
- Slice.Raw (Nil)
- Slice.Replace
- Slice.Replace (False)
- Slice.Replace (Last)
- Slice.Reverse
- Slice.Shift
- Slice.Shift (Nil)
- Slice.Shuffle
- Slice.Sort
- Slice.Sort (Nil)
- Slice.Unshift
- Slice.Unshift (Nil)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Slice ¶
type Slice[T any] []T
Slice is a wrapper of any slice that allows performing basic operations over slices using an intuitive syntax.
var s slice.Slice[int] s.Push(4, 5, 6) // [4 5 6] s.Unshift(1, 2, 3) // [1 2 3 4 5 6] s.Pop() // [1 2 3 4 5]
func (*Slice[T]) Cap ¶
Cap returns the capacity of the given slice.
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s1 := slice.Slice[int](nil)
s2 := slice.FromRaw(make([]int, 0, 100))
fmt.Println(s1.Cap())
fmt.Println(s2.Cap())
}
Output: 0 100
func (*Slice[T]) Clone ¶
Clone returns a new slice with the same length and copies to it all the elements from the existing slice.
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
original := make([]int, 2, 10)
original[0] = 1
original[1] = 2
s := slice.FromRaw(original)
clone := s.Clone()
// modify the original slice
s.DeleteOne(1)
s.Push(5)
fmt.Println(original)
fmt.Println(clone) // clone remains unchanged
}
Output: [1 5] [1 2]
Example (Nil) ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw[int](nil)
fmt.Println(s.Clone() == nil)
}
Output: true
func (*Slice[T]) Delete ¶
Delete deletes a vector of the given length under the given index from the given slice.
s := slice.FromRaw([]int{1, 2, 3, 4, 5})
s.Delete(1, 3)
fmt.Println(s) [1 5]
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw([]string{"one", "two", "three", "four", "five", "six"})
fmt.Println(s.Delete(0, 1)) // [two three four five six]
fmt.Println(s.Delete(4, 1)) // [two three four five]
fmt.Println(s.Delete(1, 2)) // [two five]
fmt.Println(s.Delete(0, 3)) // [two five] - do nothing, invalid input
fmt.Println(s)
}
Output: true true true false [two five]
Example (Last) ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw([]int{1, 2, 3, 4, 5})
s.Delete(-1 /* index */, 1 /* length */)
fmt.Println(s)
}
Output: [1 2 3 4]
func (*Slice[T]) DeleteOne ¶
DeleteOne deletes a single element from the given slice.
s.DeleteOne(index) // it's an equivalent of s.Delete(index, 1)
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw([]string{"one", "two", "three"})
fmt.Println(s.DeleteOne(3)) // no element under the index 3
fmt.Println(s.DeleteOne(1))
fmt.Println(s)
}
Output: false true [one three]
func (*Slice[T]) Empty ¶
Empty returns false when the len of the given slice equals 0.
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s1 := slice.FromRaw(make([]int, 0))
s2 := slice.Slice[int](nil)
s3 := slice.Slice[int]([]int{1})
s3.Pop()
fmt.Println(s1.Empty())
fmt.Println(s2.Empty())
fmt.Println(s3.Empty())
}
Output: true true true
func (*Slice[T]) Filter ¶
Filter filters the given slice using the provided func.
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
x := slice.FromRaw([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
x.Filter(func(_ int, val int) bool {
return val%2 == 0
})
fmt.Println(x)
}
Output: [2 4 6 8 10]
Example (Nil) ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
x := slice.FromRaw[int](nil)
x.Filter(func(index int, val int) bool { // do nothing
return true
})
fmt.Println(x.Raw() == nil)
}
Output: true
func (*Slice[T]) Get ¶
Get returns an element under the given index. It accepts negative indexes.
x := slice.FromRaw([]int{1, 2, 3, 4, 5})
val, ok := x.Get(-1)
fmt.Println(val) // 5
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
x := slice.FromRaw([]int{1, 2, 3, 4, 5})
fmt.Println(x.Get(0)) // 1 true
fmt.Println(x.Get(4)) // 5 true
fmt.Println(x.Get(-1)) // 5 true
fmt.Println(x.Get(-5)) // 1 true
fmt.Println(x.Get(-6)) // 0 false
fmt.Println(x.Get(5)) // 0 false
}
Output: 1 true 5 true 5 true 1 true 0 false 0 false
func (*Slice[T]) Insert ¶
Insert inserts the given element to the existing slice under the given index.
s := slice.FromRaw([]string{"one", "four"})
s.Insert(1 "two", "three")
fmt.Println(s) // ["one", "two", "three", "four"]
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw([]string{"one", "four"})
s.Insert(1, "two", "three")
fmt.Println(s)
}
Output: [one two three four]
Example (False) ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw[string](nil)
fmt.Println(s.Insert(1, "one")) // the highest possible index to insert == len(s)
s = slice.FromRaw([]string{"zero", "one", "two"})
fmt.Println(s.Insert(-100, "minus one")) // invalid index
}
Output: false false
func (*Slice[T]) Len ¶
Len returns the length of the given slice.
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s1 := slice.Slice[int](nil)
s2 := slice.FromRaw(make([]int, 100))
fmt.Println(s1.Len())
fmt.Println(s2.Len())
}
Output: 0 100
func (*Slice[T]) Pop ¶
Pop returns the last element and removes it from the given slice.
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw([]int{1, 2})
fmt.Println(s.Pop())
fmt.Println(s.Pop())
fmt.Println(s.Pop())
}
Output: 2 true 1 true 0 false
func (*Slice[T]) Push ¶
func (s *Slice[T]) Push(v ...T)
Push appends the given input to the given slice.
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw([]int{1, 2})
s.Push(3)
s.Push(4, 5)
fmt.Println(s)
}
Output: [1 2 3 4 5]
Example (Nil) ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw[int](nil)
s.Push(1, 2, 3)
fmt.Println(s)
}
Output: [1 2 3]
func (*Slice[T]) Raw ¶
func (s *Slice[T]) Raw() []T
Raw returns the underlying slice.
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
data := []int{1, 2, 3}
s := slice.Slice[int](data)
fmt.Println(s.Raw())
}
Output: [1 2 3]
Example (Nil) ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.Slice[int](nil)
fmt.Println(s.Raw() == nil)
}
Output: true
func (*Slice[T]) Replace ¶
Replace replaces s[index:index+len(v)] with v. It does not succeed when the given slice does not have enough elements to be replaced.
s := slice.FromRaw([]string{"one", "two", "two", "two", "two"})
s.Replace(2, "three", "four", "five")
fmt.Println(s) // [one two three four five]
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw([]string{"one", "two", "two", "two", "two"})
s.Replace(2, "three", "four", "five")
fmt.Println(s)
}
Output: [one two three four five]
Example (False) ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw([]string{"one", "two", "two", "two"})
// cannot replace 3 elements starting from index 2
// the given slice does not have enough elements
fmt.Println(s.Replace(2, "three", "four", "five"))
fmt.Println(s)
}
Output: false [one two two two]
Example (Last) ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw([]int{1, 2, 3, 4, 4})
s.Replace(-1, 5)
fmt.Println(s)
}
Output: [1 2 3 4 5]
func (*Slice[T]) Reverse ¶
func (s *Slice[T]) Reverse()
Reverse reverses order of the given slice.
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw([]int{5, 4, 3, 2, 1})
s.Reverse()
fmt.Println(s)
}
Output: [1 2 3 4 5]
func (*Slice[T]) Shift ¶
Shift returns the first element and removes it from the given slice.
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw([]int{1, 2, 3})
fmt.Println(s.Shift())
fmt.Println(s)
}
Output: 1 true [2 3]
Example (Nil) ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.Slice[int](nil)
fmt.Println(s.Shift())
}
Output: 0 false
func (*Slice[T]) Shuffle ¶
Shuffle shuffles the given input. randIntN must generate a pseudo-random number in the half-open interval [0,n).
s := slice.FromRaw([]int{1, 2, 3, 4, 5})
s.Shuffle(rand.Intn)
Example ¶
package main
import (
"fmt"
"math/rand"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw([]int{1, 2, 3, 4, 5})
s.Shuffle(rand.Intn)
fmt.Println(s) // e.g. [3 1 5 4 2]
}
func (*Slice[T]) Sort ¶
Sort sorts the given slice in ascending order as determined by the cmp function. It requires that cmp is a strict weak ordering. See https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings.
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw([]int{3, 2, 5, 4, 1})
s.Sort(func(a int, b int) int {
return a - b
})
fmt.Println(s)
}
Output: [1 2 3 4 5]
Example (Nil) ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
var s slice.Slice[int]
s.Sort(func(a int, b int) int { // do nothing, there is nothing to sort
return a - b
})
fmt.Println(s.Raw() == nil)
}
Output: true
func (*Slice[T]) Unshift ¶
func (s *Slice[T]) Unshift(v ...T)
Unshift prepends the given input to the given slice.
Example ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw([]int{4, 5, 6})
s.Unshift(2, 3)
s.Unshift(1)
s.Unshift() // do nothing
fmt.Println(s)
}
Output: [1 2 3 4 5 6]
Example (Nil) ¶
package main
import (
"fmt"
"github.com/go-slice/slice"
)
func main() {
s := slice.FromRaw[int](nil)
s.Unshift(1, 2, 3)
fmt.Println(s)
}
Output: [1 2 3]