Documentation ¶
Overview ¶
Package roslices defines various read-only functions useful with immutable slices of any type.
Example ¶
package main import ( "fmt" "github.com/phelmkamp/immut/roslices" "golang.org/x/exp/slices" ) func main() { ints1 := roslices.Freeze([]int{2, 1, 3}) if !roslices.IsSorted(ints1) { // must clone to sort ints2 := roslices.Clone(ints1) slices.Sort(ints2) fmt.Println(ints1, ints2) } }
Output: [2 1 3] [1 2 3]
Index ¶
- func BinarySearch[E constraints.Ordered](x Slice[E], target E) (int, bool)
- func BinarySearchFunc[E any](x Slice[E], target E, cmp func(E, E) int) (int, bool)
- func Clone[E any](s Slice[E]) []E
- func Compare[E constraints.Ordered](s1, s2 Slice[E]) int
- func CompareFunc[E1 any, E2 any](s1 Slice[E1], s2 Slice[E2], cmp func(E1, E2) int) int
- func Contains[E comparable](s Slice[E], v E) bool
- func Copy[E any](dst []E, src Slice[E]) int
- func Equal[E comparable](s1, s2 Slice[E]) bool
- func EqualFunc[E1, E2 any](s1 Slice[E1], s2 Slice[E2], eq func(E1, E2) bool) bool
- func Index[E comparable](s Slice[E], v E) int
- func IndexFunc[E any](s Slice[E], f func(E) bool) int
- func IsSorted[E constraints.Ordered](x Slice[E]) bool
- func IsSortedFunc[E any](x Slice[E], less func(a, b E) bool) bool
- type Slice
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BinarySearch ¶
func BinarySearch[E constraints.Ordered](x Slice[E], target E) (int, bool)
BinarySearch searches for target in a sorted slice and returns the position where target is found, or the position where target would appear in the sort order; it also returns a bool saying whether the target is really found in the slice. The slice must be sorted in increasing order.
func BinarySearchFunc ¶
BinarySearchFunc works like BinarySearch, but uses a custom comparison function. The slice must be sorted in increasing order, where "increasing" is defined by cmp. cmp(a, b) is expected to return an integer comparing the two parameters: 0 if a == b, a negative number if a < b and a positive number if a > b.
func Clone ¶
Clone returns a mutable copy of the slice. The elements are copied using assignment, so this is a shallow clone.
func Compare ¶
func Compare[E constraints.Ordered](s1, s2 Slice[E]) int
Compare compares the elements of s1 and s2. The elements are compared sequentially, starting at index 0, until one element is not equal to the other. The result of comparing the first non-matching elements is returned. If both slices are equal until one of them ends, the shorter slice is considered less than the longer one. The result is 0 if s1 == s2, -1 if s1 < s2, and +1 if s1 > s2. Comparisons involving floating point NaNs are ignored.
func CompareFunc ¶
CompareFunc is like Compare but uses a comparison function on each pair of elements. The elements are compared in increasing index order, and the comparisons stop after the first time cmp returns non-zero. The result is the first non-zero result of cmp; if cmp always returns 0 the result is 0 if len(s1) == len(s2), -1 if len(s1) < len(s2), and +1 if len(s1) > len(s2).
func Contains ¶
func Contains[E comparable](s Slice[E], v E) bool
Contains reports whether v is present in s.
func Copy ¶
Copy copies elements from a source slice into a destination slice. The source and destination may overlap. Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst).
func Equal ¶
func Equal[E comparable](s1, s2 Slice[E]) 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. Floating point NaNs are not considered equal.
func EqualFunc ¶
EqualFunc reports whether two slices are equal using a comparison function on each pair of elements. If the lengths are different, EqualFunc returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first index for which eq returns false.
func Index ¶
func Index[E comparable](s Slice[E], v E) int
Index returns the index of the first occurrence of v in s, or -1 if not present.
Types ¶
type Slice ¶
type Slice[E any] struct { // contains filtered or unexported fields }
Slice wraps a read-only slice.