slices

package module
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: May 26, 2024 License: Apache-2.0 Imports: 4 Imported by: 46

README

godoc codecov Go Report Card

slices - go slice type utilities

slices is a package doing all sorts of things with slices of stuff.

Installation

> go get github.com/go-corelibs/slices@latest

Examples

Cut

func main() {
    slice := []string{"Before", "things", "|", "After", "things"}
    before, after, found := Cut(slice, []string{"|"})
    // found == true
    // before == []string{"Before", "things"}
    // after == []string{"After", "things"}
}

Carve, CarveString

func main() {
    // slices.Carve is a generic list carving function which is most easily
    // demonstrated with a slice of runes yet useful for any comparable slice
    slice := []rune(`This is STARTa sliceEND of runes`)
    before, middle, after, found := Carve(slice, []rune("START"), []rune("END"))
    // found == true
    // before == []rune("This is ")
    // middle == []rune("a slice")
    // after == []rune(" of runes")

    // slices.CarveString is a convenient wrapper around slices.Carve geared
    // for use with ~string types, making the above slices.Carve example look
    // like this:
    s := `This is STARTa a stringEND of characters`
    b, m, a, ok := CarveString(s, "START", "END")
    // ok == true
    // before == "This is "
    // middle == "a string"
    // after == " of characters"
}

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2023 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyWithin

func AnyWithin[V comparable, S ~[]V](src S, others ...S) (present bool)

AnyWithin returns true if any of the values in the source given are present in any of the other slices given

func Append

func Append[V comparable, S ~[]V](src S, values ...V) (modified S)

Append returns a new slice appended with only values not within the src slice

func Carve added in v1.2.0

func Carve[V comparable, S ~[]V](src, start, end S) (before, middle, after S, found bool)

Carve finds the `start` and `end` markers in `src` and carves out the "before carve", "middle of start/end range" and "after carve" segments. If the `start` and `end` range is not `found` then the `before` will contain the entire `src` input

func CarveString added in v1.3.0

func CarveString[V ~string](src, start, end V) (before, middle, after V, found bool)

CarveString recasts the given ~string arguments to []rune slices, calls Carve and recasts the []rune slices returns values back to the original type

func Copy

func Copy[V interface{}, S ~[]V](slice S) (copied S)

Copy creates a new slice (array) from the given slice

func Cut added in v1.1.0

func Cut[V comparable, S ~[]V](src, sep S) (before, after S, found bool)

Cut is the slices version of strings.Cut

func DuplicateCounts

func DuplicateCounts[V comparable, S ~[]V](src S) (counts map[V]int)

DuplicateCounts returns a mapping of values and their respective counts, distinct values not included

func Equal

func Equal[V comparable, S ~[]V](src S, others ...S) (same bool)

Equal returns true if all the slices given have the same values

func IndexOf

func IndexOf[V comparable, S ~[]V](slice S, value V) (index int)

IndexOf returns the first index matching the value given

func IndexesOf

func IndexesOf[V comparable, S ~[]V](slice S, value V) (indexes []int)

IndexesOf returns a list of all indexes matching the value given

func Insert

func Insert[V interface{}, S ~[]V](slice S, at int, values ...V) (modified S)

Insert creates a new slice (array) from the given slice, with additional values inserted at the given index

func MakeLookup added in v1.6.0

func MakeLookup[V comparable](slices ...[]V) (lookup map[V]struct{})

MakeLookup creates a mapping of generic comparable slices with empty structs

func Merge

func Merge[V comparable, S ~[]V](src S, others ...S) (modified S)

Merge returns a new slice with the new values found within others appended to the src slice

func Pop

func Pop[V interface{}, S ~[]V](slice S) (modified S, value V)

Pop removes the last value from a Copy of the slice and returns it

func Present

func Present[V comparable](search V, others ...V) (present bool)

Present returns true if the search value is present in any of the other values given

func Prune

func Prune[V comparable, S ~[]V](slice S, values ...V) (modified S)

Prune removes all instances of the specified values from a copy of the given slice

func Push

func Push[V interface{}, S ~[]V](slice S, values ...V) (modified S)

Push appends the given value to a new copy of the given slice

func Remove

func Remove[V interface{}, S ~[]V](slice S, at int) (modified S)

Remove creates a new slice (array) from the given slice, with the specified index removed

func Retype

func Retype[T interface{}](src []interface{}) (slice []T, ok bool)

func Shift

func Shift[V interface{}, S ~[]V](slice S, values ...V) (modified S)

Shift prepends the given value to a new copy of the given slice

func StartsWith

func StartsWith[V comparable, S ~[]V](src S, others ...S) (same bool)

StartsWith returns true if the other slices given start with the same values as the src

func ToCamels

func ToCamels(list []string) (camel []string)

func ToKebabs

func ToKebabs(list []string) (kebabs []string)

func ToScreamingKebabs

func ToScreamingKebabs(list []string) (screamingKebabs []string)

func ToScreamingSnakes

func ToScreamingSnakes(list []string) (screamingSnakes []string)

func ToSnakes

func ToSnakes(list []string) (snakes []string)

func ToStrings added in v1.6.0

func ToStrings[V interface{}](slice []V) (values []string)

ToStrings creates a new string slice with all the given slice's values, converted to strings using fmt.Sprintf("%v", value) unless the value is of type rune, string, []byte or []rune, in which case they're simply recast as string

func Truncate

func Truncate[V interface{}, S ~[]V](slice S, length int) (truncated S)

Truncate creates a new slice (array), of specified length, from the given slice

func Unique

func Unique[V comparable, S ~[]V](src S) (unique S)

Unique returns a new slice with duplicate values omitted, maintaining order

func Unshift

func Unshift[V interface{}, S ~[]V](slice S) (modified S, value V)

Unshift removes the first value from a Copy of the slice and returns it

func Within

func Within[V comparable, S ~[]V](search V, others ...S) (present bool)

Within return true if the search value is present in any of the other slices of V given

Types

type Stack added in v1.5.0

type Stack[V interface{}] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Stack is a concurrency-safe manager for a stack of generic items

func NewStack added in v1.5.0

func NewStack[V interface{}](items ...V) *Stack[V]

NewStack creates a new Stack instance

func (*Stack[V]) First added in v1.5.0

func (s *Stack[V]) First() (item V, ok bool)

First returns the first item in the Stack

func (*Stack[V]) Get added in v1.5.0

func (s *Stack[V]) Get(idx int) (item V, ok bool)

Get returns the item at the index given

func (*Stack[V]) Last added in v1.5.0

func (s *Stack[V]) Last() (item V, ok bool)

Last returns the last item in the Stack

func (*Stack[V]) Len added in v1.5.0

func (s *Stack[V]) Len() (size int)

Len returns the size of the Stack

func (*Stack[V]) Pop added in v1.5.0

func (s *Stack[V]) Pop() (item V, ok bool)

Pop removes and returns the last item in the Stack

func (*Stack[V]) Push added in v1.5.0

func (s *Stack[V]) Push(item V)

Push appends the given item to the end of the Stack

func (*Stack[V]) Set added in v1.5.0

func (s *Stack[V]) Set(idx int, item V) (ok bool)

Set overwrites the item at the index given

func (*Stack[V]) Shift added in v1.5.0

func (s *Stack[V]) Shift(item V)

Shift prepends the given item to the start of the Stack

func (*Stack[V]) Slice added in v1.6.0

func (s *Stack[V]) Slice() (items []V)

Slice returns the stack as a slice

func (*Stack[V]) Unshift added in v1.5.0

func (s *Stack[V]) Unshift() (item V, ok bool)

Unshift removes and returns the first item in the Stack

func (*Stack[V]) Valid added in v1.6.0

func (s *Stack[V]) Valid(idx int) (valid bool)

Valid returns true if the given index is within bounds

type StackComparable added in v1.6.0

type StackComparable[V comparable] struct {
	Stack[V]
}

StackComparable is a concurrency-safe manager for a stack of generic comparable items

func NewStackComparable added in v1.6.0

func NewStackComparable[V comparable](items ...V) *StackComparable[V]

NewStackComparable creates a new StackComparable instance

func (*StackComparable[V]) Find added in v1.6.0

func (s *StackComparable[V]) Find(search V) (index int, ok bool)

Find searches the stack for the first instance of the search value

func (*StackComparable[V]) Has added in v1.6.0

func (s *StackComparable[V]) Has(search V) (ok bool)

Has returns true if this stack has the search value

func (*StackComparable[V]) Prune added in v1.6.0

func (s *StackComparable[V]) Prune(search ...V) (count int)

Prune removes the search values from the stack, returns the number of items removed

type StackUnique added in v1.6.0

type StackUnique[V comparable] struct {
	StackComparable[V]
}

StackUnique is a concurrency-safe manager for a stack of unique generic comparable items

func NewStackUnique added in v1.6.0

func NewStackUnique[V comparable](items ...V) *StackUnique[V]

NewStackUnique creates a new StackUnique instance

func (*StackUnique[V]) Push added in v1.6.0

func (s *StackUnique[V]) Push(item V)

Push appends the given item to the end of the Stack

func (*StackUnique[V]) Set added in v1.6.0

func (s *StackUnique[V]) Set(idx int, item V) (ok bool)

Set overwrites the item at the index given

func (*StackUnique[V]) Shift added in v1.6.0

func (s *StackUnique[V]) Shift(item V)

Shift prepends the given item to the start of the Stack

Jump to

Keyboard shortcuts

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