slices

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2021 License: GPL-3.0 Imports: 2 Imported by: 1

Documentation

Overview

Manipulation of slices.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddElems

func AddElems(slice []interface{}, value interface{}, indices ...int) []interface{}

Adds the given value at the given indices.

If there is an index which exceeds the length of the given slice plus the number of unique indices given then this will result in an new array that's the length of the maximum index in indices. If this happens then any "empty" space will be filled by default by "nil".

Example

Add the given element at the given indices.

arr := []interface{}{1, 2, 3}
fmt.Println("Before:", arr)

// All duplicate indices will be removed.
// Here the new length of the array will be 7 as it is greater than len(arr) + len(unique indices).
arr = AddElems(arr, 0, 0, 0, 3, 7, 1)
fmt.Println("After:", arr)
Output:
Before: [1 2 3]
After: [0 0 1 0 2 3 <nil> 0]

func RemoveDuplicatesAndSort

func RemoveDuplicatesAndSort(indices *[]int)

Remove duplicates and sort an array of integers in place.

func RemoveElems

func RemoveElems(slice []interface{}, indices ...int) []interface{}

Removes the elements at the given indices in the given interface slice and returns a new slice.

The new array will have a length which is the difference between the length of the given slice and the length of the given indices as a unique set.

Example

Remove the given indices from an array.

arr := []interface{}{1, 2, 3, 4, 5}
fmt.Println("Before:", arr)

// All duplicate indices will be removed.
arr = RemoveElems(arr, 4, 4, 2, 1, 2)
fmt.Println("After:", arr)
Output:
Before: [1 2 3 4 5]
After: [1 4]

func SameElements added in v1.2.0

func SameElements(x, y []interface{}) bool

Check if two interface slices have the same elements.

Unlike reflect.DeepEqual this will not care about order.

Example
// Two slices with the same elements but different orders
arr1 := []interface{}{1, 2, 3}
arr2 := []interface{}{2, 1, 3}
fmt.Printf("SameElements(%v, %v) = %t\n", arr1, arr2, SameElements(arr1, arr2))

// Two slices with the different elements
arr1 = []interface{}{1, 2, 4}
arr2 = []interface{}{2, 3, 1}
fmt.Printf("SameElements(%v, %v) = %t\n", arr1, arr2, SameElements(arr1, arr2))
Output:
SameElements([1 2 3], [2 1 3]) = true
SameElements([1 2 4], [2 3 1]) = false

Types

This section is empty.

Jump to

Keyboard shortcuts

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