slice

package module
Version: v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2020 License: MIT Imports: 3 Imported by: 11

README

Build Status codecov GoDoc Go Report Card golangci

Type-safe functions for common Go slice operations.

Installation

go get github.com/psampaz/slice

Operations

✔ = Supported

✕ = Non supported

- = Not yet implemented

bool byte complex(all) float(all) int(all) string uint(all) uintptr
Batch - - - - - - - -
Contains
Copy
Deduplicate
Delete
DeleteRange
Filter
Insert - - - - - - - -
Max
Min
Pop
Push - - - - - - - -
Reverse
Shift - - - - - - - -
Shuffle
Sum
Unshift - - - - - - - -

Examples

slice.Deduplicate

Deduplicate performs order preserving, in place deduplication of a slice

    a := []int{1, 2, 3, 2, 5, 3}
    a = slice.DeduplicateInt(a) // [1, 2, 3, 5]

slice.Delete

Delete removes an element at a specific index of a slice. An error is return in case the index is out of bounds or the slice is nil or empty.

    a := []int{1, 2, 3, 4, 5}
    a, err = slice.DeleteInt(a, 2) // [1, 2, 4, 5], nil

slice.DeleteRange

DeleteRange deletes the elements between from and to index (inclusive) from a slice. An error is return in case the index is out of bounds or the slice is nil or empty.

    a := []int{1, 2, 3, 4, 5}
    a, err = slice.DeleteRangeInt(a, 2, 3) // [1, 2, 5], nil

slice.Contains

Contains checks if a specific value exists in a slice.

    a := []int{1, 2, 3, 4, 5}
    exists := slice.ContainsInt(a, 3) // true

slice.Copy

Copy creates a copy of a slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

    a := []int{1, 2, 3, 4}
    b := slice.CopyInt(a) // [1, 2, 3, 4]

slice.Filter

Filter performs in place filtering of a slice based on a predicate

    a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    keep := func(x int) bool {
        return x%2 == 0 
    }
    a = slice.FilterInt(a, keep) // [2, 4, 6, 8, 10]

slice.Max

Max returns the maximum value of a slice or an error in case of a nil or empty slice.

    a := []int{1, 2, 3, 0, 4, 5}
    max, err := slice.MaxInt(a) // 5, nil

slice.Min

Min returns the minimum value of a slice or an error in case of a nil or empty slice.

    a := []int{1, 2, 3, 0, 4, 5}
    min, err := slice.MinInt(a) // 0, nil

slice.Pop

Pop removes and returns the last value a slice and the remaining slice. An error is returned in case of a nil or empty slice.

    a := []int{1, 2, 3, 4, 5}
    v, a, err := slice.PopInt(a) // 5, [1, 2, 3, 4], nil

slice.Reverse

Reverse performs in place reversal of a slice

    a := []int{1, 2, 3, 4, 5}
    a = slice.ReverseInt(a) // [5, 4, 3, 2, 1]

slice.Shuffle

Shuffle shuffles (in place) a slice

    a := []int{1, 2, 3, 4, 5}
    a = slice.ShuffleInt(a) // [3, 5, 1, 4, 2] (random output)

slice.Sum

Sum returns the sum of the values of a slice or an error in case of a nil or empty slice

    a := []int{1, 2, 3}
    sum, err := slice.SumInt(a) // 6, nil

Tests

if you want to run the test suite for this library:

$ go test -v -cover

Credits

Contributing

You are very welcome to contribute new operations or bug fixes in this library.

Contribution guidelines (code)

  1. Use only functions. This is a function based library so struct based operations will not be accepted, in order to preserve simplicity and consistency.

  2. If the operation is not working on a nil or empty slice, then the function should return an error.

  3. If the operation accepts slice indexes as parameters, then the function should guard against out of bound index values and return an error in that case.

  4. All operations should be in place operations, meaning that they should alter the original slice.

  5. Each function should have precise documentation.

  6. Each operation should live in each own file. Example:

    min.go
    min_test.go
    
  7. The naming convention for functions is OperationType. Example:

    MinInt32()
    

    instead of

    Int32Min()
    
  8. Implement ALL applicable types in the same PR.

  9. Include one testable example for Int type at the end of the test file.

  10. Include one example in the Examples section of README

  11. Update the table in the Operation section of README

  12. Update the UNRELEASED section of CHANGELOG

Contribution guidelines (tests)

  1. All code should be 100% covered with tests
  2. All operations should be tested for 3 scenarios at least:
    1. nil slice
    2. empty slice
    3. non empty slice

Static code analysis

golangci.com runs on all PRs. Code is checked with golint, go vet, gofmt, plus 20+ linters, and review comments will be automatically added in your PR in case of a failure. You can see the whole list of linters here: https://golangci.com/product#linters

Steps for contributing new operations

  1. Open an issue describing the new operation, the proposed name and the applicable types.
  2. If the operation is approved to be included in the library, create a small PR the implementation and test for only only type.
  3. After code review you can proceed the implementation for the rest types. This is necessary because if you submit a PR with the implementation and test for all types, a small correction during review could eventually lead to a big refactor due to code duplication.

Using Genny for fast implementation of all types of an operation (Optional)

The following steps are an example of how to use https://github.com/cheekybits/genny to implement the min operation:

  1. Install Genny

    go get github.com/cheekybits/genny
    
  2. Create a file named min_genny.go

    package slice
    
    import (
        "errors"
        "github.com/cheekybits/genny/generic"
    )
    
    type Type generic.Type
    
    // MinType returns the minimum value of an Type slice or an error in case of a nil or empty slice
    func MinType(a []Type) (Type, error) {
        if len(a) == 0 {
            return 0, errors.New("Cannot get the minimum of a nil or empty slice")
        }
    
        min := a[0]
        for k := 1; k < len(a); k++ {
            if a[k] < min {
                min = a[k]
            }
        }
    
        return min, nil
    }
    
  3. Use genny to generate code for all Go's built in types:

    cat min_genny.go | genny gen Type=BUILTINS > min.go
    

    This step will generate a file min.go with the following content:

    package slice
    
    import "errors"
    
    // MinByte returns the minimum value of a byte slice or an error in case of a nil or empty slice
    func MinByte(a []byte) (byte, error) {
        if len(a) == 0 {
            return 0, errors.New("Cannot get the minimum of a nil or empty slice")
        }
    
        min := a[0]
        for k := 1; k < len(a); k++ {
            if a[k] < min {
                min = a[k]
            }
        }
    
        return min, nil
    }
    
    // MinFloat32 returns the minimum value of a float32 slice or an error in case of a nil or empty slice
    func MinFloat32(a []float32) (float32, error) {
        if len(a) == 0 {
            return 0, errors.New("Cannot get the minimum of a nil or empty slice")
        }
    
        min := a[0]
        for k := 1; k < len(a); k++ {
            if a[k] < min {
                min = a[k]
            }
        }
    
        return min, nil
    }
    .
    .
    .
    .
    
  4. Delete the implementation for all types not applicable for the operation

  5. Create a file named min_genny_test.go

    package slice
    
    import (
        "fmt"
        "testing"
    )
    
    func TestMinType(t *testing.T) {
        type args struct {
            a []Type
        }
        tests := []struct {
            name    string
            args    args
            want    Type
            wantErr bool
        }{
            {
                name: "nil slice",
                args: args{
                    a: nil,
                },
                want:    0,
                wantErr: true,
            },
            {
                name: "empty slice",
                args: args{
                    a: []Type{},
                },
                want:    0,
                wantErr: true,
            },
            {
                name: "non empty slice",
                args: args{
                    a: []Type{1, 3, 2, 0, 5, 4},
                },
                want:    0,
                wantErr: false,
            },
        }
        for _, tt := range tests {
            t.Run(tt.name, func(t *testing.T) {
                got, err := MinType(tt.args.a)
                if (err != nil) != tt.wantErr {
                    t.Errorf("MinType() error = %v, wantErr %v", err, tt.wantErr)
                    return
                }
                if got != tt.want {
                    t.Errorf("MinType() = %v, want %v", got, tt.want)
                }
            })
        }
    }
    
    
  6. Use genny to generate tests for all Go's built in types:

    cat min_genny_test.go | genny gen Type=BUILTINS > min_test.go
    

    This step will generate a file min_test.go with tests for each one of Go's built in types.

  7. Remove tests for non applicable types.

  8. Adjust the tests for each one of the types.

  9. Delete min_genny.go and min_genny_test.go

Documentation

Overview

Package slice provides typesafe functions for common Go slice operations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsBool

func ContainsBool(a []bool, x bool) bool

ContainsBool checks if a value exists in a bool slice

func ContainsByte

func ContainsByte(a []byte, x byte) bool

ContainsByte checks if a value exists in a byte slice

func ContainsComplex128

func ContainsComplex128(a []complex128, x complex128) bool

ContainsComplex128 checks if a value exists in a complex128 slice

func ContainsComplex64

func ContainsComplex64(a []complex64, x complex64) bool

ContainsComplex64 checks if a value exists in a complex64 slice

func ContainsFloat32

func ContainsFloat32(a []float32, x float32) bool

ContainsFloat32 checks if a value exists in a float32 slice

func ContainsFloat64

func ContainsFloat64(a []float64, x float64) bool

ContainsFloat64 checks if a value exists in a float64 slice

func ContainsInt

func ContainsInt(a []int, x int) bool

ContainsInt checks if a value exists in an int slice

Example
a := []int{1, 2, 3, 0, 7, 5, 2}
contains := ContainsInt(a, 7)
fmt.Printf("%v", contains)
Output:

true

func ContainsInt16

func ContainsInt16(a []int16, x int16) bool

ContainsInt16 checks if a value exists in an int16 slice

func ContainsInt32

func ContainsInt32(a []int32, x int32) bool

ContainsInt32 checks if a value exists in an int32 slice

func ContainsInt64

func ContainsInt64(a []int64, x int64) bool

ContainsInt64 checks if a value exists in an int64 slice

func ContainsInt8

func ContainsInt8(a []int8, x int8) bool

ContainsInt8 checks if a value exists in an int8 slice

func ContainsRune

func ContainsRune(a []rune, x rune) bool

ContainsRune checks if a value exists in a rune slice

func ContainsString

func ContainsString(a []string, x string) bool

ContainsString checks if a value exists in a string slice

func ContainsUint

func ContainsUint(a []uint, x uint) bool

ContainsUint checks if a value exists in a uint slice

func ContainsUint16

func ContainsUint16(a []uint16, x uint16) bool

ContainsUint16 checks if a value exists in a uint16 slice

func ContainsUint32

func ContainsUint32(a []uint32, x uint32) bool

ContainsUint32 checks if a value exists in a uint32 slice

func ContainsUint64

func ContainsUint64(a []uint64, x uint64) bool

ContainsUint64 checks if a value exists in a uint64 slice

func ContainsUint8

func ContainsUint8(a []uint8, x uint8) bool

ContainsUint8 checks if a value exists in a uint8 slice

func ContainsUintptr

func ContainsUintptr(a []uintptr, x uintptr) bool

ContainsUintptr checks if a value exists in a uintptr slice

func CopyBool

func CopyBool(a []bool) []bool

CopyBool creates a copy of a bool slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyByte

func CopyByte(a []byte) []byte

CopyByte creates a copy of a byte slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyComplex128

func CopyComplex128(a []complex128) []complex128

CopyComplex128 creates a copy of a complex128 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyComplex64

func CopyComplex64(a []complex64) []complex64

CopyComplex64 creates a copy of a complex64 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyFloat32

func CopyFloat32(a []float32) []float32

CopyFloat32 creates a copy of a float32 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyFloat64

func CopyFloat64(a []float64) []float64

CopyFloat64 creates a copy of a float64 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyInt

func CopyInt(a []int) []int

CopyInt creates a copy of an int slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyInt16

func CopyInt16(a []int16) []int16

CopyInt16 creates a copy of an int16 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyInt32

func CopyInt32(a []int32) []int32

CopyInt32 creates a copy of an int32 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyInt64

func CopyInt64(a []int64) []int64

CopyInt64 creates a copy of an int64 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyInt8

func CopyInt8(a []int8) []int8

CopyInt8 creates a copy of an int8 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyRune

func CopyRune(a []rune) []rune

CopyRune creates a copy of a rune slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyString

func CopyString(a []string) []string

CopyString creates a copy of a string slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyUint

func CopyUint(a []uint) []uint

CopyUint creates a copy of a uint slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyUint16

func CopyUint16(a []uint16) []uint16

CopyUint16 creates a copy of a uint16 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyUint32

func CopyUint32(a []uint32) []uint32

CopyUint32 creates a copy of a uint32 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyUint64

func CopyUint64(a []uint64) []uint64

CopyUint64 creates a copy of a uint64 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyUint8

func CopyUint8(a []uint8) []uint8

CopyUint8 creates a copy of a uint8 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func CopyUintptr

func CopyUintptr(a []uintptr) []uintptr

CopyUintptr creates a copy of a uintptr slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

func DeduplicateBool

func DeduplicateBool(a []bool) []bool

DeduplicateBool performs order preserving, in place deduplication of a bool slice

func DeduplicateByte

func DeduplicateByte(a []byte) []byte

DeduplicateByte performs order preserving, in place deduplication of a byte slice

func DeduplicateComplex128

func DeduplicateComplex128(a []complex128) []complex128

DeduplicateComplex128 performs order preserving, in place deduplication of a complex128 slice

func DeduplicateComplex64

func DeduplicateComplex64(a []complex64) []complex64

DeduplicateComplex64 performs order preserving, in place deduplication of a complex64 slice

func DeduplicateFloat32

func DeduplicateFloat32(a []float32) []float32

DeduplicateFloat32 performs order preserving, in place deduplication of a float32 slice

func DeduplicateFloat64

func DeduplicateFloat64(a []float64) []float64

DeduplicateFloat64 performs order preserving, in place deduplication of a float64 slice

func DeduplicateInt

func DeduplicateInt(a []int) []int

DeduplicateInt performs order preserving, in place deduplication of a int slice

Example
a := []int{1, 2, 3, 2, 5, 3}
a = DeduplicateInt(a)
fmt.Printf("%v", a)
Output:

[1 2 3 5]

func DeduplicateInt16

func DeduplicateInt16(a []int16) []int16

DeduplicateInt16 performs order preserving, in place deduplication of a int16 slice

func DeduplicateInt32

func DeduplicateInt32(a []int32) []int32

DeduplicateInt32 performs order preserving, in place deduplication of a int32 slice

func DeduplicateInt64

func DeduplicateInt64(a []int64) []int64

DeduplicateInt64 performs order preserving, in place deduplication of a int64 slice

func DeduplicateInt8

func DeduplicateInt8(a []int8) []int8

DeduplicateInt8 performs order preserving, in place deduplication of a int8 slice

func DeduplicateRune

func DeduplicateRune(a []rune) []rune

DeduplicateRune performs order preserving, in place deduplication of a rune slice

func DeduplicateString

func DeduplicateString(a []string) []string

DeduplicateString performs order preserving, in place deduplication of a string slice

func DeduplicateUint

func DeduplicateUint(a []uint) []uint

DeduplicateUint performs order preserving, in place deduplication of a uint slice

func DeduplicateUint16

func DeduplicateUint16(a []uint16) []uint16

DeduplicateUint16 performs order preserving, in place deduplication of a uint16 slice

func DeduplicateUint32

func DeduplicateUint32(a []uint32) []uint32

DeduplicateUint32 performs order preserving, in place deduplication of a uint32 slice

func DeduplicateUint64

func DeduplicateUint64(a []uint64) []uint64

DeduplicateUint64 performs order preserving, in place deduplication of a uint64 slice

func DeduplicateUint8

func DeduplicateUint8(a []uint8) []uint8

DeduplicateUint8 performs order preserving, in place deduplication of a uint8 slice

func DeduplicateUintptr

func DeduplicateUintptr(a []uintptr) []uintptr

DeduplicateUintptr performs order preserving, in place deduplication of a uintptr slice

func DeleteBool added in v0.2.0

func DeleteBool(a []bool, i int) ([]bool, error)

DeleteBool removes an element at a specific index of a bool slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteByte added in v0.2.0

func DeleteByte(a []byte, i int) ([]byte, error)

DeleteByte removes an element at a specific index of a byte slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteComplex128 added in v0.2.0

func DeleteComplex128(a []complex128, i int) ([]complex128, error)

DeleteComplex128 removes an element at a specific index of a complex128 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteComplex64 added in v0.2.0

func DeleteComplex64(a []complex64, i int) ([]complex64, error)

DeleteComplex64 removes an element at a specific index of a complex64 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteFloat32 added in v0.2.0

func DeleteFloat32(a []float32, i int) ([]float32, error)

DeleteFloat32 removes an element at a specific index of a float32 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteFloat64 added in v0.2.0

func DeleteFloat64(a []float64, i int) ([]float64, error)

DeleteFloat64 removes an element at a specific index of a float64 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteInt added in v0.2.0

func DeleteInt(a []int, i int) ([]int, error)

DeleteInt removes an element at a specific index of an int slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

Example
a := []int{1, 2, 3, 4, 5}
a, err := DeleteInt(a, 2)
fmt.Printf("%v, %v", a, err)
Output:

[1 2 4 5], <nil>

func DeleteInt16 added in v0.2.0

func DeleteInt16(a []int16, i int) ([]int16, error)

DeleteInt16 removes an element at a specific index of an int16 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteInt32 added in v0.2.0

func DeleteInt32(a []int32, i int) ([]int32, error)

DeleteInt32 removes an element at a specific index of an int32 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteInt64 added in v0.2.0

func DeleteInt64(a []int64, i int) ([]int64, error)

DeleteInt64 removes an element at a specific index of an int64 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteInt8 added in v0.2.0

func DeleteInt8(a []int8, i int) ([]int8, error)

DeleteInt8 removes an element at a specific index of an int8 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteRangeBool added in v0.2.0

func DeleteRangeBool(a []bool, from, to int) ([]bool, error)

DeleteRangeBool deletes the elements between from and to index (inclusive) from a bool slice

func DeleteRangeByte added in v0.2.0

func DeleteRangeByte(a []byte, from, to int) ([]byte, error)

DeleteRangeByte deletes the elements between from and to index (inclusive) from a byte slice

func DeleteRangeComplex128 added in v0.2.0

func DeleteRangeComplex128(a []complex128, from, to int) ([]complex128, error)

DeleteRangeComplex128 deletes the elements between from and to index (inclusive) from a complex128 slice

func DeleteRangeComplex64 added in v0.2.0

func DeleteRangeComplex64(a []complex64, from, to int) ([]complex64, error)

DeleteRangeComplex64 deletes the elements between from and to index (inclusive) from a complex64 slice

func DeleteRangeFloat32 added in v0.2.0

func DeleteRangeFloat32(a []float32, from, to int) ([]float32, error)

DeleteRangeFloat32 deletes the elements between from and to index (inclusive) from a float32 slice

func DeleteRangeFloat64 added in v0.2.0

func DeleteRangeFloat64(a []float64, from, to int) ([]float64, error)

DeleteRangeFloat64 deletes the elements between from and to index (inclusive) from a float64 slice

func DeleteRangeInt added in v0.2.0

func DeleteRangeInt(a []int, from, to int) ([]int, error)

DeleteRangeInt deletes the elements between from and to index (inclusive) from a int slice

Example
a := []int{1, 2, 3, 4, 5}
a, err := DeleteRangeInt(a, 2, 3)
fmt.Printf("%v, %v", a, err)
Output:

[1 2 5], <nil>

func DeleteRangeInt16 added in v0.2.0

func DeleteRangeInt16(a []int16, from, to int) ([]int16, error)

DeleteRangeInt16 deletes the elements between from and to index (inclusive) from a int16 slice

func DeleteRangeInt32 added in v0.2.0

func DeleteRangeInt32(a []int32, from, to int) ([]int32, error)

DeleteRangeInt32 deletes the elements between from and to index (inclusive) from a int32 slice

func DeleteRangeInt64 added in v0.2.0

func DeleteRangeInt64(a []int64, from, to int) ([]int64, error)

DeleteRangeInt64 deletes the elements between from and to index (inclusive) from a int64 slice

func DeleteRangeInt8 added in v0.2.0

func DeleteRangeInt8(a []int8, from, to int) ([]int8, error)

DeleteRangeInt8 deletes the elements between from and to index (inclusive) from a int8 slice

func DeleteRangeRune added in v0.2.0

func DeleteRangeRune(a []rune, from, to int) ([]rune, error)

DeleteRangeRune deletes the elements between from and to index (inclusive) from a rune slice

func DeleteRangeString added in v0.2.0

func DeleteRangeString(a []string, from, to int) ([]string, error)

DeleteRangeString deletes the elements between from and to index (inclusive) from a string slice

func DeleteRangeUint added in v0.2.0

func DeleteRangeUint(a []uint, from, to int) ([]uint, error)

DeleteRangeUint deletes the elements between from and to index (inclusive) from a uint slice

func DeleteRangeUint16 added in v0.2.0

func DeleteRangeUint16(a []uint16, from, to int) ([]uint16, error)

DeleteRangeUint16 deletes the elements between from and to index (inclusive) from a uint16 slice

func DeleteRangeUint32 added in v0.2.0

func DeleteRangeUint32(a []uint32, from, to int) ([]uint32, error)

DeleteRangeUint32 deletes the elements between from and to index (inclusive) from a uint32 slice

func DeleteRangeUint64 added in v0.2.0

func DeleteRangeUint64(a []uint64, from, to int) ([]uint64, error)

DeleteRangeUint64 deletes the elements between from and to index (inclusive) from a uint64 slice

func DeleteRangeUint8 added in v0.2.0

func DeleteRangeUint8(a []uint8, from, to int) ([]uint8, error)

DeleteRangeUint8 deletes the elements between from and to index (inclusive) from a uint8 slice

func DeleteRangeUintptr added in v0.2.0

func DeleteRangeUintptr(a []uintptr, from, to int) ([]uintptr, error)

DeleteRangeUintptr deletes the elements between from and to index (inclusive) from a uintptr slice

func DeleteRune added in v0.2.0

func DeleteRune(a []rune, i int) ([]rune, error)

DeleteRune removes an element at a specific index of a rune slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteString added in v0.2.0

func DeleteString(a []string, i int) ([]string, error)

DeleteString removes an element at a specific index of a string slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteUint added in v0.2.0

func DeleteUint(a []uint, i int) ([]uint, error)

DeleteUint removes an element at a specific index of a uint slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteUint16 added in v0.2.0

func DeleteUint16(a []uint16, i int) ([]uint16, error)

DeleteUint16 removes an element at a specific index of a uint16 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteUint32 added in v0.2.0

func DeleteUint32(a []uint32, i int) ([]uint32, error)

DeleteUint32 removes an element at a specific index of a uint32 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteUint64 added in v0.2.0

func DeleteUint64(a []uint64, i int) ([]uint64, error)

DeleteUint64 removes an element at a specific index of a uint64 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteUint8 added in v0.2.0

func DeleteUint8(a []uint8, i int) ([]uint8, error)

DeleteUint8 removes an element at a specific index of a uint8 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func DeleteUintptr added in v0.2.0

func DeleteUintptr(a []uintptr, i int) ([]uintptr, error)

DeleteUintptr removes an element at a specific index of a uintptr slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

func FilterBool

func FilterBool(a []bool, keep func(x bool) bool) []bool

FilterBool performs in place filtering of a bool slice based on a predicate

func FilterByte

func FilterByte(a []byte, keep func(x byte) bool) []byte

FilterByte performs in place filtering of a byte slice based on a predicate

func FilterComplex128

func FilterComplex128(a []complex128, keep func(x complex128) bool) []complex128

FilterComplex128 performs in place filtering of a complex128 slice based on a predicate

func FilterComplex64

func FilterComplex64(a []complex64, keep func(x complex64) bool) []complex64

FilterComplex64 performs in place filtering of a complex64 slice based on a predicate

func FilterFloat32

func FilterFloat32(a []float32, keep func(x float32) bool) []float32

FilterFloat32 performs in place filtering of a float32 slice based on a predicate

func FilterFloat64

func FilterFloat64(a []float64, keep func(x float64) bool) []float64

FilterFloat64 performs in place filtering of a float64 slice based on a predicate

func FilterInt

func FilterInt(a []int, keep func(x int) bool) []int

FilterInt performs in place filtering of an int slice based on a predicate

Example
a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
keep := func(x int) bool {
	return x%2 == 0
}
a = FilterInt(a, keep)
fmt.Println(a)
// Output [2, 4, 6, 8 , 10]
Output:

func FilterInt16

func FilterInt16(a []int16, keep func(x int16) bool) []int16

FilterInt16 performs in place filtering of an int16 slice based on a predicate

func FilterInt32

func FilterInt32(a []int32, keep func(x int32) bool) []int32

FilterInt32 performs in place filtering of an int32 slice based on a predicate

func FilterInt64

func FilterInt64(a []int64, keep func(x int64) bool) []int64

FilterInt64 performs in place filtering of an int64 slice based on a predicate

func FilterInt8

func FilterInt8(a []int8, keep func(x int8) bool) []int8

FilterInt8 performs in place filtering of an int8 slice based on a predicate

func FilterRune

func FilterRune(a []rune, keep func(x rune) bool) []rune

FilterRune performs in place filtering of a rune slice based on a predicate

func FilterString

func FilterString(a []string, keep func(x string) bool) []string

FilterString performs in place filtering of a string slice based on a predicate

func FilterUint

func FilterUint(a []uint, keep func(x uint) bool) []uint

FilterUint performs in place filtering of a uint slice based on a predicate

func FilterUint16

func FilterUint16(a []uint16, keep func(x uint16) bool) []uint16

FilterUint16 performs in place filtering of a uint16 slice based on a predicate

func FilterUint32

func FilterUint32(a []uint32, keep func(x uint32) bool) []uint32

FilterUint32 performs in place filtering of a uint32 slice based on a predicate

func FilterUint64

func FilterUint64(a []uint64, keep func(x uint64) bool) []uint64

FilterUint64 performs in place filtering of a uint64 slice based on a predicate

func FilterUint8

func FilterUint8(a []uint8, keep func(x uint8) bool) []uint8

FilterUint8 performs in place filtering of a uint8 slice based on a predicate

func FilterUintptr

func FilterUintptr(a []uintptr, keep func(x uintptr) bool) []uintptr

FilterUintptr performs in place filtering of a uintptr slice based on a predicate

func MaxByte

func MaxByte(a []byte) (byte, error)

MaxByte returns the maximum value of a byte slice or an error in case of a nil or empty slice

func MaxFloat32

func MaxFloat32(a []float32) (float32, error)

MaxFloat32 returns the maximum value of a float32 slice or an error in case of a nil or empty slice

func MaxFloat64

func MaxFloat64(a []float64) (float64, error)

MaxFloat64 returns the maximum value of a float64 slice or an error in case of a nil or empty slice

func MaxInt

func MaxInt(a []int) (int, error)

MaxInt returns the maximum value of a int slice or an error in case of a nil or empty slice

Example
a := []int{1, 2, 3, 0, 7, 5, 2}
max, err := MaxInt(a)
fmt.Printf("%d, %v", max, err)
Output:

7, <nil>

func MaxInt16

func MaxInt16(a []int16) (int16, error)

MaxInt16 returns the maximum value of a int16 slice or an error in case of a nil or empty slice

func MaxInt32

func MaxInt32(a []int32) (int32, error)

MaxInt32 returns the maximum value of a int32 slice or an error in case of a nil or empty slice

func MaxInt64

func MaxInt64(a []int64) (int64, error)

MaxInt64 returns the maximum value of a int64 slice or an error in case of a nil or empty slice

func MaxInt8

func MaxInt8(a []int8) (int8, error)

MaxInt8 returns the maximum value of a int8 slice or an error in case of a nil or empty slice

func MaxRune

func MaxRune(a []rune) (rune, error)

MaxRune returns the maximum value of a rune slice or an error in case of a nil or empty slice

func MaxUint

func MaxUint(a []uint) (uint, error)

MaxUint returns the maximum value of a uint slice or an error in case of a nil or empty slice

func MaxUint16

func MaxUint16(a []uint16) (uint16, error)

MaxUint16 returns the maximum value of a uint16 slice or an error in case of a nil or empty slice

func MaxUint32

func MaxUint32(a []uint32) (uint32, error)

MaxUint32 returns the maximum value of a uint32 slice or an error in case of a nil or empty slice

func MaxUint64

func MaxUint64(a []uint64) (uint64, error)

MaxUint64 returns the maximum value of a uint64 slice or an error in case of a nil or empty slice

func MaxUint8

func MaxUint8(a []uint8) (uint8, error)

MaxUint8 returns the maximum value of a uint8 slice or an error in case of a nil or empty slice

func MaxUintptr

func MaxUintptr(a []uintptr) (uintptr, error)

MaxUintptr returns the maximum value of a uintptr slice or an error in case of a nil or empty slice

func MinByte

func MinByte(a []byte) (byte, error)

MinByte returns the minimum value of a byte slice or an error in case of a nil or empty slice

func MinFloat32

func MinFloat32(a []float32) (float32, error)

MinFloat32 returns the minimum value of a float32 slice or an error in case of a nil or empty slice

func MinFloat64

func MinFloat64(a []float64) (float64, error)

MinFloat64 returns the minimum value of a float64 slice or an error in case of a nil or empty slice

func MinInt

func MinInt(a []int) (int, error)

MinInt returns the minimum value of an int slice or an error in case of a nil or empty slice

Example
a := []int{1, 2, 3, 0, 7, 5, 2}
min, err := MinInt(a)
fmt.Printf("%d, %v", min, err)
Output:

0, <nil>

func MinInt16

func MinInt16(a []int16) (int16, error)

MinInt16 returns the minimum value of an int16 slice or an error in case of a nil or empty slice

func MinInt32

func MinInt32(a []int32) (int32, error)

MinInt32 returns the minimum value of an int32 slice or an error in case of a nil or empty slice

func MinInt64

func MinInt64(a []int64) (int64, error)

MinInt64 returns the minimum value of an int64 slice or an error in case of a nil or empty slice

func MinInt8

func MinInt8(a []int8) (int8, error)

MinInt8 returns the minimum value of an int8 slice or an error in case of a nil or empty slice

func MinRune

func MinRune(a []rune) (rune, error)

MinRune returns the minimum value of a rune slice or an error in case of a nil or empty slice

func MinUint

func MinUint(a []uint) (uint, error)

MinUint returns the minimum value of a uint slice or an error in case of a nil or empty slice

func MinUint16

func MinUint16(a []uint16) (uint16, error)

MinUint16 returns the minimum value of a uint16 slice or an error in case of a nil or empty slice

func MinUint32

func MinUint32(a []uint32) (uint32, error)

MinUint32 returns the minimum value of a uint32 slice or an error in case of a nil or empty slice

func MinUint64

func MinUint64(a []uint64) (uint64, error)

MinUint64 returns the minimum value of a uint64 slice or an error in case of a nil or empty slice

func MinUint8

func MinUint8(a []uint8) (uint8, error)

MinUint8 returns the minimum value of a uint8 slice or an error in case of a nil or empty slice

func MinUintptr

func MinUintptr(a []uintptr) (uintptr, error)

MinUintptr returns the minimum value of a uintptr slice or an error in case of a nil or empty slice

func PopBool added in v0.2.0

func PopBool(a []bool) (bool, []bool, error)

PopBool removes and returns the last value a bool slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopByte added in v0.2.0

func PopByte(a []byte) (byte, []byte, error)

PopByte removes and returns the last value a byte slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopComplex128 added in v0.2.0

func PopComplex128(a []complex128) (complex128, []complex128, error)

PopComplex128 removes and returns the last value a complex128 slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopComplex64 added in v0.2.0

func PopComplex64(a []complex64) (complex64, []complex64, error)

PopComplex64 removes and returns the last value a complex64 slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopFloat32 added in v0.2.0

func PopFloat32(a []float32) (float32, []float32, error)

PopFloat32 removes and returns the last value a float32 slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopFloat64 added in v0.2.0

func PopFloat64(a []float64) (float64, []float64, error)

PopFloat64 removes and returns the last value a float64 slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopInt added in v0.2.0

func PopInt(a []int) (int, []int, error)

PopInt removes and returns the last value a int slice and the remaining slice. An error is returned in case of a nil or empty slice.

Example
a := []int{1, 2, 3, 4, 5}
v, a, err := PopInt(a)
fmt.Printf("%d, %v, %v", v, a, err)
Output:

5, [1 2 3 4], <nil>

func PopInt16 added in v0.2.0

func PopInt16(a []int16) (int16, []int16, error)

PopInt16 removes and returns the last value a int16 slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopInt32 added in v0.2.0

func PopInt32(a []int32) (int32, []int32, error)

PopInt32 removes and returns the last value a int32 slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopInt64 added in v0.2.0

func PopInt64(a []int64) (int64, []int64, error)

PopInt64 removes and returns the last value a int64 slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopInt8 added in v0.2.0

func PopInt8(a []int8) (int8, []int8, error)

PopInt8 removes and returns the last value a int8 slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopRune added in v0.2.0

func PopRune(a []rune) (rune, []rune, error)

PopRune removes and returns the last value a rune slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopString added in v0.2.0

func PopString(a []string) (string, []string, error)

PopString removes and returns the last value a string slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopUint added in v0.2.0

func PopUint(a []uint) (uint, []uint, error)

PopUint removes and returns the last value a uint slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopUint16 added in v0.2.0

func PopUint16(a []uint16) (uint16, []uint16, error)

PopUint16 removes and returns the last value a uint16 slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopUint32 added in v0.2.0

func PopUint32(a []uint32) (uint32, []uint32, error)

PopUint32 removes and returns the last value a uint32 slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopUint64 added in v0.2.0

func PopUint64(a []uint64) (uint64, []uint64, error)

PopUint64 removes and returns the last value a uint64 slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopUint8 added in v0.2.0

func PopUint8(a []uint8) (uint8, []uint8, error)

PopUint8 removes and returns the last value a uint8 slice and the remaining slice. An error is returned in case of a nil or empty slice.

func PopUintptr added in v0.2.0

func PopUintptr(a []uintptr) (uintptr, []uintptr, error)

PopUintptr removes and returns the last value a uintptr slice and the remaining slice. An error is returned in case of a nil or empty slice.

func ReverseBool

func ReverseBool(a []bool) []bool

ReverseBool performs in place reversal of a bool slice

func ReverseByte

func ReverseByte(a []byte) []byte

ReverseByte performs in place reversal of a byte slice

func ReverseComplex128

func ReverseComplex128(a []complex128) []complex128

ReverseComplex128 performs in place reversal of a complex128 slice

func ReverseComplex64

func ReverseComplex64(a []complex64) []complex64

ReverseComplex64 performs in place reversal of a complex64 slice

func ReverseFloat32

func ReverseFloat32(a []float32) []float32

ReverseFloat32 performs in place reversal of a float32 slice

func ReverseFloat64

func ReverseFloat64(a []float64) []float64

ReverseFloat64 performs in place reversal of a float64 slice

func ReverseInt

func ReverseInt(a []int) []int

ReverseInt performs in place reversal of an int slice

Example
a := []int{1, 2, 3, 4, 5}
a = ReverseInt(a)
fmt.Printf("%v", a)
Output:

[5 4 3 2 1]

func ReverseInt16

func ReverseInt16(a []int16) []int16

ReverseInt16 performs in place reversal of an int16 slice

func ReverseInt32

func ReverseInt32(a []int32) []int32

ReverseInt32 performs in place reversal of an int32 slice

func ReverseInt64

func ReverseInt64(a []int64) []int64

ReverseInt64 performs in place reversal of an int64 slice

func ReverseInt8

func ReverseInt8(a []int8) []int8

ReverseInt8 performs in place reversal of an int8 slice

func ReverseRune

func ReverseRune(a []rune) []rune

ReverseRune performs in place reversal of a rune slice

func ReverseString

func ReverseString(a []string) []string

ReverseString performs in place reversal of a string slice

func ReverseUint

func ReverseUint(a []uint) []uint

ReverseUint performs in place reversal of a uint slice

func ReverseUint16

func ReverseUint16(a []uint16) []uint16

ReverseUint16 performs in place reversal of a uint16 slice

func ReverseUint32

func ReverseUint32(a []uint32) []uint32

ReverseUint32 performs in place reversal of a uint32 slice

func ReverseUint64

func ReverseUint64(a []uint64) []uint64

ReverseUint64 performs in place reversal of a uint64 slice

func ReverseUint8

func ReverseUint8(a []uint8) []uint8

ReverseUint8 performs in place reversal of a uint8 slice

func ReverseUintptr

func ReverseUintptr(a []uintptr) []uintptr

ReverseUintptr performs in place reversal of a uintptr slice

func ShuffleBool added in v0.2.0

func ShuffleBool(a []bool) []bool

ShuffleBool shuffles (in place) a bool slice

func ShuffleByte added in v0.2.0

func ShuffleByte(a []byte) []byte

ShuffleByte shuffles (in place) a byte slice

func ShuffleComplex128 added in v0.2.0

func ShuffleComplex128(a []complex128) []complex128

ShuffleComplex128 shuffles (in place) a complex128 slice

func ShuffleComplex64 added in v0.2.0

func ShuffleComplex64(a []complex64) []complex64

ShuffleComplex64 shuffles (in place) a complex64 slice

func ShuffleFloat32 added in v0.2.0

func ShuffleFloat32(a []float32) []float32

ShuffleFloat32 shuffles (in place) a float32 slice

func ShuffleFloat64 added in v0.2.0

func ShuffleFloat64(a []float64) []float64

ShuffleFloat64 shuffles (in place) a float64 slice

func ShuffleInt added in v0.2.0

func ShuffleInt(a []int) []int

ShuffleInt shuffles (in place) a int slice

func ShuffleInt16 added in v0.2.0

func ShuffleInt16(a []int16) []int16

ShuffleInt16 shuffles (in place) a int16 slice

func ShuffleInt32 added in v0.2.0

func ShuffleInt32(a []int32) []int32

ShuffleInt32 shuffles (in place) a int32 slice

func ShuffleInt64 added in v0.2.0

func ShuffleInt64(a []int64) []int64

ShuffleInt64 shuffles (in place) a int64 slice

func ShuffleInt8 added in v0.2.0

func ShuffleInt8(a []int8) []int8

ShuffleInt8 shuffles (in place) a int8 slice

func ShuffleRune added in v0.2.0

func ShuffleRune(a []rune) []rune

ShuffleRune shuffles (in place) a rune slice

func ShuffleString added in v0.2.0

func ShuffleString(a []string) []string

ShuffleString shuffles (in place) a string slice

func ShuffleUint added in v0.2.0

func ShuffleUint(a []uint) []uint

ShuffleUint shuffles (in place) a uint slice

func ShuffleUint16 added in v0.2.0

func ShuffleUint16(a []uint16) []uint16

ShuffleUint16 shuffles (in place) a uint16 slice

func ShuffleUint32 added in v0.2.0

func ShuffleUint32(a []uint32) []uint32

ShuffleUint32 shuffles (in place) a uint32 slice

func ShuffleUint64 added in v0.2.0

func ShuffleUint64(a []uint64) []uint64

ShuffleUint64 shuffles (in place) a uint64 slice

func ShuffleUint8 added in v0.2.0

func ShuffleUint8(a []uint8) []uint8

ShuffleUint8 shuffles (in place) a uint8 slice

func ShuffleUintptr added in v0.2.0

func ShuffleUintptr(a []uintptr) []uintptr

ShuffleUintptr shuffles (in place) a uintptr slice

func SumByte

func SumByte(a []byte) (byte, error)

SumByte returns the sum of the values of a byte Slice or an error in case of a nil or empty slice

func SumComplex128

func SumComplex128(a []complex128) (complex128, error)

SumComplex128 returns the sum of the values of a complex128 Slice or an error in case of a nil or empty slice

func SumComplex64

func SumComplex64(a []complex64) (complex64, error)

SumComplex64 returns the sum of the values of a complex64 Slice or an error in case of a nil or empty slice

func SumFloat32

func SumFloat32(a []float32) (float32, error)

SumFloat32 returns the sum of the values of a float32 Slice or an error in case of a nil or empty slice

func SumFloat64

func SumFloat64(a []float64) (float64, error)

SumFloat64 returns the sum of the values of a float64 Slice or an error in case of a nil or empty slice

func SumInt

func SumInt(a []int) (int, error)

SumInt returns the sum of the values of a int Slice or an error in case of a nil or empty slice

Example
a := []int{1, 2, 3}
sum, err := SumInt(a)
fmt.Printf("%d, %v", sum, err)
Output:

6, <nil>

func SumInt16

func SumInt16(a []int16) (int16, error)

SumInt16 returns the sum of the values of a int16 Slice or an error in case of a nil or empty slice

func SumInt32

func SumInt32(a []int32) (int32, error)

SumInt32 returns the sum of the values of a int32 Slice or an error in case of a nil or empty slice

func SumInt64

func SumInt64(a []int64) (int64, error)

SumInt64 returns the sum of the values of a int64 Slice or an error in case of a nil or empty slice

func SumInt8

func SumInt8(a []int8) (int8, error)

SumInt8 returns the sum of the values of a int8 Slice or an error in case of a nil or empty slice

func SumRune

func SumRune(a []rune) (rune, error)

SumRune returns the sum of the values of a rune Slice or an error in case of a nil or empty slice

func SumUint

func SumUint(a []uint) (uint, error)

SumUint returns the sum of the values of a uint Slice or an error in case of a nil or empty slice

func SumUint16

func SumUint16(a []uint16) (uint16, error)

SumUint16 returns the sum of the values of a uint16 Slice or an error in case of a nil or empty slice

func SumUint32

func SumUint32(a []uint32) (uint32, error)

SumUint32 returns the sum of the values of a uint32 Slice or an error in case of a nil or empty slice

func SumUint64

func SumUint64(a []uint64) (uint64, error)

SumUint64 returns the sum of the values of a uint64 Slice or an error in case of a nil or empty slice

func SumUint8

func SumUint8(a []uint8) (uint8, error)

SumUint8 returns the sum of the values of a uint8 Slice or an error in case of a nil or empty slice

func SumUintptr

func SumUintptr(a []uintptr) (uintptr, error)

SumUintptr returns the sum of the values of a uintptr Slice or an error in case of a nil or empty slice

Types

This section is empty.

Jump to

Keyboard shortcuts

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