array

package
v0.0.0-...-91bf12d Latest Latest
Warning

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

Go to latest
Published: May 5, 2025 License: MIT Imports: 6 Imported by: 0

README

📦 array

Utilities for manipulating and inspecting slices in Go — built for robust data processing and transformation.


✨ Features

  • Generic support for comparable, any, and constraints.Ordered
  • Safe removal by value or index
  • Advanced operations: filter, map, reduce, group, partition, etc.
  • Immutable operations — no in-place mutation
  • Utility helpers like Zip, Shuffle, MinMax, and more

📚 Usage

✅ Contains
ok := array.Contains([]int{1, 2, 3}, 2) // true

🧹 Remove

newSlice := array.RemoveByValue([]string{"a", "b", "c"}, "b")

🧠 Map & Filter

squared := array.Map([]int{1, 2, 3}, func(v int) int { return v * v })
evens := array.Filter([]int{1, 2, 3, 4}, func(v int) bool { return v%2 == 0 })

🔀 Shuffle & Split

shuffled := array.Shuffle([]int{1, 2, 3})
chunks, _ := array.Split([]int{1, 2, 3, 4}, 2)

🧮 Reduce & Group

sum := array.Reduce([]int{1, 2, 3}, func(acc, v int) int { return acc + v }, 0)
grouped := array.GroupBy([]string{"go", "rust", "ruby"}, func(v string) int { return len(v) })

📦 Installation

go get github.com/jwm1rr0rb/faraway_lib_test_task/backend/golang/array

📝 License


🙌 Contributions

  • PRs and suggestions welcome! Feel free to fork, open issues, or create a pull request 🚀

  • Let me know if you want badges, a table of contents, or markdown export to file!

Documentation

Overview

Package array provides utilities for manipulating and inspecting slices, with a focus on data processing and transformation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AreIdentical

func AreIdentical[T comparable](x, y []T) bool

AreIdentical checks if two slices contain the same elements with identical counts. Works with any comparable type.

func Concat

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

Concat combines multiple slices into a single slice. Works with any type.

func Contains

func Contains[T comparable](s []T, e T) bool

Contains checks if an element is present in a slice. Works with any comparable type (int, string, etc.).

func DistinctCount

func DistinctCount[T comparable](s []T) map[T]int

DistinctCount returns a map of unique elements to their frequencies. Works with any comparable type.

func Every

func Every[T any](s []T, test func(T) bool) bool

Every checks if all elements in a slice satisfy the predicate.

func Filter

func Filter[T any](s []T, keep func(T) bool) []T

Filter returns a new slice with elements that satisfy the predicate.

func Find

func Find[T any](s []T, match func(T) bool) (T, bool)

Find returns the first element in a slice that satisfies the predicate. Returns the element and a boolean indicating if it was found.

func GroupBy

func GroupBy[T any, K comparable](s []T, keyFunc func(T) K) map[K][]T

GroupBy groups elements by a key function, returning a map of key to slice. The key type K must be comparable; T can be any type.

func IndexOf

func IndexOf[T comparable](s []T, value T) int

IndexOf returns the first index of a value in a slice, or -1 if not found. Works with any comparable type.

func Join

func Join[T any](s []T, sep string) string

Join concatenates slice elements into a string with a separator. Works with any type that can be formatted with %v.

func JoinString

func JoinString[T fmt.Stringer](s []T, sep string) string

JoinString concatenates elements using their String() method. Requires T to implement fmt.Stringer.

func Map

func Map[T, U any](s []T, transform func(T) U) []U

Map applies a transformation function to each element, returning a new slice.

func MinMax

func MinMax[T constraints.Ordered](s []T) (min T, max T, err error)

MinMax finds the minimum and maximum values in a slice. Works with ordered types (int, float64, string, etc.).

func Partition

func Partition[T any](s []T, test func(T) bool) (pass, fail []T)

Partition splits a slice into two based on a predicate. Returns elements that pass and fail the test, respectively.

func Reduce

func Reduce[T, U any](s []T, reducer func(U, T) U, initial U) U

Reduce applies a reduction function over the slice, accumulating a result. Starts with an initial value; T and U can be any types.

func RemoveByIndex

func RemoveByIndex[T any](s []T, index int) ([]T, error)

RemoveByIndex removes an element at the specified index from a slice. Returns the new slice and an error if index is out of bounds. Does not modify the original slice.

func RemoveByValue

func RemoveByValue[T comparable](s []T, value T) []T

RemoveByValue removes the first occurrence of a value from a slice. Returns the new slice or the original if value not found. Does not modify the original slice.

func Reverse

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

Reverse returns a new slice with elements in reversed order.

func Shuffle

func Shuffle[T any](s []T) []T

Shuffle randomly reorders a slice. Uses math/rand/v2 for randomization; does not modify the original slice.

func Some

func Some[T any](s []T, test func(T) bool) bool

Some checks if at least one element in a slice satisfies the predicate.

func Sort

func Sort[T any](s []T, less func(a, b T) bool) []T

Sort sorts the slice using the provided less function. For ordered types, use: Sort(s, func(a, b T) bool { return a < b })

func Split

func Split[T any](s []T, size int) ([][]T, error)

Split divides a slice into chunks of the specified size. The last chunk may be smaller if the length is not evenly divisible.

func Take

func Take[T any](s []T, n int) ([]T, error)

Take returns the first n elements of a slice. Returns all if n >= len(s), or an empty slice with error if n < 0.

func Uniq

func Uniq[T comparable](s []T) []T

Uniq removes duplicates from a slice, preserving order. Works with any comparable type.

Types

type Pair

type Pair[T, U any] struct {
	First  T
	Second U
}

Pair represents a pair of values of two different types.

func Zip

func Zip[T, U any](s1 []T, s2 []U) []Pair[T, U]

Zip combines two slices into a slice of Pair structs. Stops at the length of the shorter slice.

Jump to

Keyboard shortcuts

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