slice

package
v0.0.0-...-96aff74 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package slice provides a set of functions to manipulate slices.

Example

import "github.com/neurocode-io/go-pkgs/slice"

input := []int{1, 2, 3}
slice.Reverse(input)
// input == []int{3, 2, 1}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](slice []T, fn keepFunc[T]) bool

All returns true if all elements in the slice satisfy the given predicate.

Example

input := []string{"a", "b", "c"}
fn := func(s string) bool { return len(s) == 1 }
slice.All(input, fn)
// true

func Any

func Any[T any](slice []T, fn keepFunc[T]) bool

Any returns true if any element in the slice satisfies the given predicate.

Example

input := []string{"a", "b", "c"}
fn := func(s string) bool { return s == "b" }
slice.Any(input, fn)
// true

func Contains

func Contains[T comparable](slice []T, value T) bool

Contains returns true if the slice contains the value.

Example

input := []string{"a", "b", "c"}
result := slice.Contains(input, "b")
// result == true

func Filter

func Filter[T any](slice []T, fn keepFunc[T]) []T

Filter returns a new slice containing only the elements of the input slice that satisfy the given predicate.

Example

input := []int{1, 2, 3, 4, 5}
fn := func(i int) bool { return i%2 == 0 }
result := slice.Filter(input, fn)
// result == []int{2, 4}

func Find

func Find[T comparable](slice []T, fn keepFunc[T]) (T, bool)

Find returns the first element that matches the function. If no element matches the function, the zero value is returned.

Example

input := []string{"a", "b", "c"}
fn := func(s string) bool {
	return s == "b"
}
slice.Find(input, fn)
// input == "b"

func FindIndex

func FindIndex[T comparable](slice []T, fn keepFunc[T]) (int, bool)

FindIndex returns the index of the first element that matches the function. If no element matches the function, -1 is returned.

Example

input := []string{"a", "b", "c"}
fn := func(s string) bool {
		return s == "b"
}
slice.FindIndex(input, fn)
// input == 1

func Intersection

func Intersection[T comparable](slices ...[]T) []T

func Map

func Map[T any](slice []T, fn mapFunc[T]) []T

Map returns a new slice with the result of applying the function to each element.

Example

input := []int{1, 2, 3}
fn := func(i int) int { return i * 2 }
result := slice.Map(input, fn)
// result == []int{2, 4, 6}

func Merge

func Merge[T any](slices ...[]T) []T

Merge merges multiple slices into a single slice.

Example

input1 := []int{1, 2, 3}
input2 := []int{4, 5, 6}
result := slice.Merge(input1, input2)
// result == []int{1, 2, 3, 4, 5, 6}

func None

func None[T any](slice []T, fn keepFunc[T]) bool

None returns true if no elements in the slice satisfy the given predicate.

Example

input := []string{"a", "b", "c"}
fn := func(s string) bool { return len(s) == 2 }
slice.None(input, fn)
// true

func Reduce

func Reduce[T, V any](slice []T, accumulator V, fn func(V, T) V) V

Reduce reduces the slice to a single value.

The function is called with an initial value sometimes called an accumulator and each element in the slice. The result of the function is passed as the accumulator to the next call.

Example

input := []int{1, 2, 3}
fn := func(acc int, i int) int { return acc + i }
result := slice.Reduce(input, 0, fn)
// result == 6

func Reverse

func Reverse[T any](slice []T) []T

Reverse returns a reversed copy of the slice.

Example

input := []string{"a", "b", "c"}
result := slice.Reverse(input)
// result == []string{"c", "b", "a"}

func Sort

func Sort[T constraints.Ordered](slice []T) []T

Sort returns a sorted copy of the slice.

Example

input := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}
result := slice.Sort(input)
// result == []int{1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9}

func Union

func Union[T comparable](slices ...[]T) []T

func Unique

func Unique[T comparable](slice []T) []T

Unique returns a new slice with unique elements.

Example

input := []string{"a", "b", "c", "c", "b", "a"}
slice.Unique(input)
// input == []string{"a", "b", "c"}

func UniqueBy

func UniqueBy[T comparable](slice []T, fn mapFunc[T]) []T

UniqueBy returns a new slice with unique elements based on the function.

Example

input := []string{"a", "b", "c", "aa", "bb", "cc"}
fn := func(s string) string {
	return s[:1]
}
slice.UniqueBy(input, fn)
// input == []string{"a", "b", "c"}

Types

This section is empty.

Jump to

Keyboard shortcuts

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