slicelib

package module
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2024 License: MIT Imports: 4 Imported by: 0

README

Slicelib

This repository contains a collection of utility functions and types for working with slices in Go. These utilities provide convenient methods for manipulating and comparing slices, making your Go code more efficient and readable.

The minimum required version of go is 1.18.

Installation

You can install my module with this command

go get github.com/Tom5521/slicelib@latest

Usage

Examples
package main

import (
	"fmt"
	"strings"

	"github.com/Tom5521/slicelib"
)

func main(){
	a := slicelib.NewSlice("a1", "a2", "a3", "b1", "b2", "b3")

	a.Filter(func(s string) bool {
		return strings.HasPrefix(s, "a")
	})

fmt.Println(a) // Output: [a1 a2 a3]
}
package main

import (
	"fmt"

	"github.com/Tom5521/slicelib"
)

func main() {
	a := slicelib.NewSlice("a1", "a2", "a3", "b1", "b2", "b3")

	contains := a.Contains("b2")

	fmt.Println(contains) // Output: true
}
The Slice types has the following methods:
  • At
  • Elem (deprecated)
  • S
  • SliceP
  • Append
  • Clear
  • Copy
  • Index
  • Insert
  • Delete
  • Pop
  • Remove
  • Reverse
  • IsEmpty
  • Len
  • Contains
  • RemoveDuplicates
  • Equal
  • EqualSlice
  • EqualFunc
  • EqualSliceFunc
  • SortFunc
  • Filter
  • Range
Only on OrderedSlice:
  • BinarySearch
  • Sort
  • IsSorted

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ComparableSlice

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

func NewComparableSlice

func NewComparableSlice[T comparable](slice ...T) ComparableSlice[T]

Creates a new object of ComparableSlice wich only implements the comparable interface.

func (ComparableSlice[T]) Contains

func (s ComparableSlice[T]) Contains(v T) bool

A shortcut to slices.Contains.

func (ComparableSlice[T]) Copy

func (s ComparableSlice[T]) Copy() ComparableSlice[T]

Creates a copy of the current object, which is not the same as the current object.

implements the slices.Clone function on the internal slice to create the new structure.

func (ComparableSlice[T]) Equal

func (s ComparableSlice[T]) Equal(v []T) bool

A shortcut to slices.Equal.

func (ComparableSlice[T]) Index

func (s ComparableSlice[T]) Index(v T) int

A shortcut to slices.Index.

type OrderedSlice

type OrderedSlice[T cmp.Ordered] struct {
	ComparableSlice[T]
}

func NewOrderedSlice

func NewOrderedSlice[T cmp.Ordered](slice ...T) OrderedSlice[T]

Create a new OrderedSlice object. Which only implements the cmp.Ordered and comparable interfaces.

func (OrderedSlice[T]) BinarySearch

func (s OrderedSlice[T]) BinarySearch(v T) (int, bool)

A shortcut to slices.BinarySearch.

func (OrderedSlice[T]) Copy

func (s OrderedSlice[T]) Copy() OrderedSlice[T]

Creates a copy of the current object, which is not the same as the current object.

implements the slices.Clone function on the internal slice to create the new structure.

func (OrderedSlice[T]) IsSorted added in v1.1.0

func (s OrderedSlice[T]) IsSorted() bool

A shortcut to slices.IsSorted.

func (*OrderedSlice[T]) Sort

func (s *OrderedSlice[T]) Sort()

A shortcut to slices.Sort.

type Slice

type Slice[T any] struct {
	// contains filtered or unexported fields
}

func NewSlice

func NewSlice[T any](slice ...T) Slice[T]

Creates a new object of type Slice.

func (*Slice[T]) Append

func (s *Slice[T]) Append(items ...T)

Equivalent to slice = append(slice,myElems...).

func (Slice[T]) At added in v1.4.1

func (s Slice[T]) At(index int) T

Return the element of the given index.

func (*Slice[T]) Clear

func (s *Slice[T]) Clear()

Removes all slice elements leaving a slice with length 0 and 0 elements, but not nil.

func (Slice[T]) Contains

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

Returns true if the slice contains the element, comparing with slices.ContainsFunc and reflect.DeepEqual.

func (Slice[T]) Copy

func (s Slice[T]) Copy() Slice[T]

Creates a copy of the current object, which is not the same as the current object.

implements the slices.Clone function on the internal slice to create the new structure.

func (*Slice[T]) Delete

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

Call the slices.Delete function with the internal slice.

func (Slice[T]) Elem deprecated added in v1.2.0

func (s Slice[T]) Elem(index int) T

Return the element of the given index.

Deprecated: Uses Slice.At instead.

func (Slice[T]) Equal

func (s Slice[T]) Equal(v []T) bool

Returns a slices.EqualFunc compared with reflect.DeepEqual.

func (Slice[T]) EqualFunc

func (s Slice[T]) EqualFunc(v []T, f func(e1, e2 T) bool) bool

A shortcut to slices.EqualFunc.

func (Slice[T]) EqualSlice added in v1.6.0

func (s Slice[T]) EqualSlice(v Slice[T]) bool

func (Slice[T]) EqualSliceFunc added in v1.6.0

func (s Slice[T]) EqualSliceFunc(v Slice[T], f func(e1, e2 T) bool) bool

func (*Slice[T]) Filter

func (s *Slice[T]) Filter(f func(T) bool)

Delete all elements that do not match the logic of the function.

func (Slice[T]) Index

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

Performs a slices.IndexFunc comparing it with reflect.DeepEqual on the internal slice, returning the result.

Returns the first occurrence of the supplied element.

func (*Slice[T]) Insert

func (s *Slice[T]) Insert(index int, items ...T)

Call the slices.Insert function with the internal slice.

func (Slice[T]) IsEmpty

func (s Slice[T]) IsEmpty() bool

Return if the slice length is 0.

func (Slice[T]) Len

func (s Slice[T]) Len() int

Returns the length of the slice.

func (*Slice[T]) Pop

func (s *Slice[T]) Pop(index int)

Removes only the provided index.

func (*Slice[T]) Range added in v1.4.0

func (s *Slice[T]) Range(yield func(k int, v T) bool)

An implementation of iter.Seq2 for go 1.23 or later.

func (*Slice[T]) Remove

func (s *Slice[T]) Remove(v T)

Removes the first occurrence of the provided element.

func (*Slice[T]) RemoveDuplicates

func (s *Slice[T]) RemoveDuplicates()

Removes duplicates from the slice making all elements unique.

func (*Slice[T]) Reverse

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

Call the slices.Reverse function with the internal slice.

func (Slice[T]) S added in v1.3.0

func (s Slice[T]) S() []T

Return the built-in slice.

func (*Slice[T]) SliceP

func (s *Slice[T]) SliceP() *[]T

Return the pointer of the golang built-in slice.

func (*Slice[T]) SortFunc

func (s *Slice[T]) SortFunc(f func(a, b T) int)

A direct access to slices.SortFunc.

func (Slice[T]) String added in v1.6.0

func (s Slice[T]) String() (txt string)

Jump to

Keyboard shortcuts

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