slice

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: Apache-2.0 Imports: 0 Imported by: 0

README

Slice

Installation

go get github.com/jin06/pkg/slice

Examples

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chunk

func Chunk[T any](source []T, size int) (dest [][]T)

Chunk Creates an array of elements split into groups the length of size.If array can't be split evenly, the final chunk will be the remaining elements.

Example
anySource := []any{1, 2, 3, "jk", true, 0.069}
intSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
stringSource := []string{"a", "b", "c"}
fmt.Println(Chunk(anySource, 2))
fmt.Println(Chunk(intSource, 5))
fmt.Println(Chunk(stringSource, 2))
fmt.Println(Chunk([]struct{}{}, 1))
Output:

[[1 2] [3 jk] [true 0.069]]
[[1 2 3 4 5] [6 7 8 9]]
[[a b] [c]]
[]

func Difference added in v0.0.8

func Difference[T comparable](first []T, second []T) (result []T)

Difference Creates an array of array values not included in the other given arrays using SameValueZero for equality comparisons. The order and references of result values are determined by the first array.

Example
first := []int{1, 2, 3}
second := []int{2, 1, 4}
fmt.Println(Difference(first, second))
fmt.Println(Difference([]string{"19", "teck", "!"}, []string{"teck", "!"}))
Output:

[3]
[19]

func Drop added in v0.0.8

func Drop[T any](array []T, n int) (result []T)

Drop Creates a slice of array with n elements dropped from the beginning while n >=0 and the ending while n < 0.

Example
array := []int{1, 3, 4, 5}
fmt.Println(Drop(array, 1))

array2 := []string{"start", "foo", "bar", "end"}
fmt.Println(Drop(array2, -1))
Output:

[1 4 5]
[start foo bar]

func DropWhile added in v0.0.9

func DropWhile[T any](array []T, iterateFn func(element T) bool) (result []T)

DropWhile Creates a slice of array excluding elements dropped. Drop the elements that iterateFn result is true and return the other elements as a new slice.

Example
fn := func(i int) bool {
	return i%2 == 1
}
array := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(DropWhile(array, fn))
type vehicle struct {
	Brand string
	Class string
}
fn2 := func(v vehicle) bool {
	return v.Class == "suv" || v.Brand == "Ford"
}
array2 := []vehicle{
	{
		Brand: "Ford",
		Class: "car",
	},
	{
		Brand: "Ford",
		Class: "suv",
	},
	{
		Brand: "BMW",
		Class: "suv",
	},
	{
		Brand: "BMW",
		Class: "car",
	},
	{
		Brand: "Toyota",
		Class: "car",
	},
	{
		Brand: "Toyota",
		Class: "mpv",
	},
}
fmt.Println(DropWhile(array2, fn2))
Output:

[2 4 6 8]
[{BMW car} {Toyota car} {Toyota mpv}]

func Equal added in v0.0.8

func Equal[T comparable](a, b T) bool

func Fill added in v0.0.9

func Fill[T any](array []T, value T) (result []T)

Fill Fills elements of slice with value.

Example
arr := make([]string, 10)
fmt.Println(Fill(arr, "default"))
Output:

[default default default default default default default default default default]

func FindIndex added in v0.0.9

func FindIndex[T any](source []T, fn func(element T) bool) int

FindIndex This method returns the index of the first element fn returns truthy for instead of the element itself.

Example
type user struct {
	Name string
	Age  int
}
datas := []user{
	{
		Name: "A",
		Age:  19,
	},
	{
		Name: "B",
		Age:  21,
	},
	{
		Name: "C",
		Age:  29,
	},
}

fmt.Println(FindIndex(datas, func(element user) bool {
	if element.Age > 20 && element.Age < 30 && element.Name != "" {
		return true
	}
	return false
}))
Output:

1

func FindLastIndex added in v0.0.9

func FindLastIndex[T any](source []T, fn func(element T) bool) int

FindLastIndex This method is like FindIndex, it iterates over elements of a slice from right to left.

Example
datas := []string{
	"apple",
	"banana",
	"abc",
}
fmt.Println(FindLastIndex(datas, func(element string) bool {
	return strings.HasPrefix(element, "a")
}))
Output:

2

func IndexOf added in v0.0.9

func IndexOf[T comparable](array []T, value T, from int) int

IndexOf Gets the index at which the first occurrence of value is found in array. If from is negative, it's used as the offset from the end of array.

Example
arr1 := []int{2, 5, 1, 5, 67, 5, 6}
fmt.Println(IndexOf(arr1, 5, 0))
fmt.Println(IndexOf(arr1, 5, 2))
fmt.Println(IndexOf(arr1, 5, -3))

arr2 := []string{"a", "b", "a", "c"}
fmt.Println(IndexOf(arr2, "b", 0))
Output:

1
3
5
1

func Intersection added in v0.0.9

func Intersection[T comparable](arrays ...[]T) (result []T)

Intersection Creates an array of unique values that are included in all given arrays. The order and references of result values are determined by the first array.

Example
arrays := [][]int{
	{1, 2, 3, 4, 5, 6},
	{5, 4, 2},
	{3, 2, 1, 5},
}

fmt.Println(Intersection(arrays...))
Output:

[2 5]

func LastIndexOf added in v0.0.9

func LastIndexOf[T comparable](array []T, value T, from int) int

LastIndexOf This method is like IndexOf except that it iterates over elements of array from right to left.

Example
arr1 := []int{2, 3, 2, 3, 1}
fmt.Println(LastIndexOf(arr1, 3, 0))
fmt.Println(LastIndexOf(arr1, 3, 2))
fmt.Println(LastIndexOf(arr1, 3, -3))
Output:

3
1
1

func Map added in v0.0.11

func Map[V any](data []V, iteratee func(val V) V) []V

Map running each element in data thru iteratee, and return a new arrays.

Example
data := []int{1, 2, 3, 4}

result := Map(data, func(val int) int {
	result := val + 1
	return result
})
fmt.Println(result)
Output:

[2 3 4 5]

func Reduce added in v0.0.11

func Reduce[V any, R any](data []V, iteratee func(result R, val V) R) R

Reduce

Example
data := []int{1, 2, 3, 4}
result := Reduce(data, func(result int, val int) int {
	return result + val
})
fmt.Println(result)
Output:

10

func Remove added in v0.0.10

func Remove[T comparable](array []T, values ...T) (result []T)

Remove removes all given values from array.

Example
array := []int{1, 3, 4, 5, 6, 3, 4}
values := []int{1, 5, 6}
fmt.Println(Remove(array, values...))
Output:

[3 4 3 4]

func RemoveWhile added in v0.0.10

func RemoveWhile[T any](array []T, fn func(e T) bool) (result []T)

RemoveWhile is equivalent to DropWhile

func Reverse added in v0.0.11

func Reverse[T any](array []T) (result []T)

Reverse array so that the first element becomes the last, the second element becomes the second to last, and so on.

Example
array := []any{1, 2, 3, 4, 5, 6, "one", "two"}
fmt.Println(Reverse(array))
Output:

[two one 6 5 4 3 2 1]

func Uniq added in v0.0.11

func Uniq[T comparable](array []T) (result []T)

Uniq Creates a duplicate-free version of an array,in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array.

Example
array := []string{"apple", "banana", "orange", "banana", "apple"}

fmt.Println(Uniq(array))
Output:

[apple banana orange]

func Xor added in v0.0.11

func Xor[T comparable](arrays ...[]T) (result []T)

Xor Creates an array of unique values. The order of result values is determined by the order they occur in the arrays.

Example
arr1 := []string{"one", "two", ",", ".", "!", "ok"}
arr2 := []string{"two", ".", "one", "hello", "123"}

arr3 := []string{"one", ".", ",", "!", "kk", "09"}

fmt.Println(Xor(arr1))
fmt.Println(Xor(arr1, arr2))
fmt.Println(Xor(arr1, arr2, arr3))
Output:

[]
[, ! ok]
[ok]

Types

This section is empty.

Jump to

Keyboard shortcuts

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