pkg

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Concat

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

Concat concatenates multiple slices into a single slice. Parameters:

  • slices: A variadic parameter that accepts multiple slices of any type

Returns:

  • A new slice containing all elements from the provided slices

func CopyWithin

func CopyWithin[T any](in []T, target int, start int, end ...int) []T

CopyWithin copies part of a slice to another location in the same slice without modifying its length. Similar to JavaScript's Array.copyWithin().

Parameters:

  • in: The slice to modify in place
  • target: Index at which to copy the elements to
  • start: Index at which to start copying from
  • end (optional): Index to stop copying (exclusive). Defaults to len(in) if not provided.

Returns:

  • The same slice with modified elements.

func Entries

func Entries[K comparable, V any](m map[K]V) []struct {
	Key   K
	Value V
}

Entries returns a slice of key-value pairs from a map. Similar to JavaScript's Object.entries().

Parameters:

  • m: The map from which to extract key-value pairs

Returns:

  • A slice of struct objects containing Key and Value fields

func Every

func Every[T any](in []T, pred func(T) bool) bool

Every tests whether all elements in the slice pass the test implemented by the provided function. Similar to JavaScript's Array.every().

Parameters:

  • in: The input slice to test
  • pred: A function that tests each element and returns a boolean

Returns:

  • true if all elements pass the test; otherwise, false

func EveryIndex

func EveryIndex[T any](in []T, pred func(T) bool) (bool, int)

EveryIndex tests whether all elements in the slice pass the test implemented by the provided function. It also returns the index of the first element that fails the test. Similar to JavaScript's Array.every().

Parameters:

  • in: The input slice to test
  • pred: A function that tests each element and returns a boolean

Returns:

  • true if all elements pass the test; otherwise, false
  • The index of the first element that fails the test, or -1 if all pass

func Fill

func Fill[T any](template []T, value T) []T

Fill creates a new slice with all elements set to the specified value. Similar to JavaScript's Array.fill().

Parameters:

  • in: The input slice to fill
  • value: The value to fill the slice with

Returns:

  • A new slice with all elements set to the specified value

func Filter

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

Filter creates a new slice with all elements that pass the test implemented by the provided function. Similar to JavaScript's Array.filter().

Parameters:

  • in: The input slice to filter
  • keep: A function that tests each element and returns true to keep it in the result

Returns:

  • A new slice containing only the elements that passed the test

func Find

func Find[T any](in []T, pred func(T) bool) (T, bool)

Find returns the first element in the provided slice that satisfies the provided testing function. Similar to JavaScript's Array.find().

Parameters:

  • in: The input slice to search
  • pred: A function that tests each element and returns true when the desired element is found

Returns:

  • The found element and true if an element passes the test; otherwise, zero value and false

func FindIndex

func FindIndex[T any](in []T, pred func(T) bool) int

FindIndex returns the index of the first element in the provided slice that satisfies the provided testing function. Similar to JavaScript's Array.findIndex().

Parameters:

  • in: The input slice to search
  • pred: A function that tests each element and returns true when the desired element is found

Returns:

  • The index of the first element that satisfies the predicate if found; otherwise, -1

func Flat

func Flat[T any](chunks [][]T) []T

Flat creates a new slice with all sub-slice elements concatenated into it recursively. Similar to JavaScript's Array.flat().

Parameters:

  • chunks: A slice of slices to flatten

Returns:

  • A new flattened slice containing all elements from the sub-slices

func FlatMap

func FlatMap[T any, R any](in []T, fn func(T) []R) []R

FlatMap first maps each element using a mapping function, then flattens the result into a new slice. Similar to JavaScript's Array.flatMap().

Parameters:

  • in: The input slice to transform
  • fn: A function that maps each element to a slice of elements of type R

Returns:

  • A new flattened slice of type R containing all mapped elements

func ForEach

func ForEach[T any](in []T, fn func(T))

ForEach executes a provided function once for each slice element. Similar to JavaScript's Array.forEach().

Parameters:

  • in: The input slice to iterate over
  • fn: A function to execute for each element

Returns:

  • None (void)

func Includes

func Includes[T comparable](in []T, target T) bool

Includes determines whether a slice includes a certain value among its entries. Similar to JavaScript's Array.includes().

Parameters:

  • in: The input slice to search
  • target: The value to search for

Returns:

  • true if the target value is found; otherwise, false

func IndexOf

func IndexOf[T comparable](in []T, target T) int

IndexOf returns the first index at which a given element can be found in the slice. Similar to JavaScript's Array.indexOf().

Parameters:

  • in: The input slice to search
  • target: The value to locate

Returns:

  • The first index of the element in the slice if found; otherwise, -1

func IndexOfFunc

func IndexOfFunc[T any](in []T, pred func(T) bool) int

IndexOfFunc returns the first index at which an element satisfies the provided testing function.

Parameters:

  • in: The input slice to search
  • pred: A function that tests each element and returns true when the desired element is found

Returns:

  • The first index of an element that satisfies the predicate if found; otherwise, -1

func Join

func Join[T fmt.Stringer](in []T, sep string) string

Join appends item to the end of in and returns the resulting slice. It behaves like JavaScript’s Array.push()/concat(); it does NOT produce a string the way Array.join() does.

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys returns a slice containing the keys of the map. Similar to JavaScript's Object.keys().

Parameters:

  • m: The map from which to extract keys

Returns:

  • A slice containing all the keys from the map

func LastIndexOf

func LastIndexOf[T comparable](in []T, target T) int

LastIndexOf returns the last index at which a given element can be found in the slice. Similar to JavaScript's Array.lastIndexOf().

Parameters:

  • in: The input slice to search
  • target: The value to locate

Returns:

  • The last index of the element in the slice if found; otherwise, -1

func LastIndexOfFunc

func LastIndexOfFunc[T any](in []T, pred func(T) bool) int

LastIndexOfFunc returns the last index for which the predicate returns true.

Parameters:

  • in: The input slice to search
  • pred: A function that tests each element and returns true when the desired element is

Returns:

  • The last index of an element that satisfies the predicate if found; otherwise, - -1

Note: This function iterates from the end of the slice to the beginning, returning the first index where the predicate returns true. If no such element is found, it returns -1.

Example usage:

index := LastIndexOfFunc([]int{1, 2, 3, 2, 1}, func(x int) bool { return x == 2 })
fmt.Println(index) // Output: 3

This function is useful for finding the last occurrence of an element that meets a specific condition.

func Map

func Map[T any, R any](in []T, f func(T) R) []R

Map applies a transformation function to each element in a slice and returns a new slice containing the results. Similar to JavaScript's Array.map().

Parameters:

  • in: The input slice to transform
  • f: A function that transforms each element from type T to type R

Returns:

  • A new slice of type R containing the transformed elements

func Pop

func Pop[T any](in []T) (T, []T)

Pop removes the last element from a slice and returns that element and the modified slice. Similar to JavaScript's Array.pop().

Parameters:

  • in: The input slice

Returns:

  • The removed element and the modified slice

func PopFree

func PopFree[T any](in []T) (T, []T)

PopFree removes the last element from a slice and returns that element and the modified slice. It additionally frees memory if the slice capacity is significantly larger than its length.

Parameters:

  • in: The input slice

Returns:

  • The removed element and the modified slice with potentially reduced capacity

func Reduce

func Reduce[T, R any](in []T, init R, fn func(R, T) R) R

Reduce executes a reducer function on each element of the slice, resulting in a single output value. Similar to JavaScript's Array.reduce().

Parameters:

  • in: The input slice to reduce
  • init: The initial value for the accumulator
  • fn: A function that takes an accumulator and the current element and returns a new accumulator

Returns:

  • The final value of the accumulator

func ReduceRight

func ReduceRight[T, R any](in []T, init R, fn func(R, T) R) R

ReduceRight works like Reduce, but processes the slice from right to left. Similar to JavaScript's Array.reduceRight().

Parameters:

  • in: The input slice to reduce
  • init: The initial value for the accumulator
  • fn: A function that takes an accumulator and the current element and returns a new accumulator

Returns:

  • The final value of the accumulator

func Reverse

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

Reverse returns a new slice with the elements in reversed order. Note: Unlike JavaScript's Array.reverse(), this does not modify the original slice in-place.

Parameters:

  • in: The input slice to reverse

Returns:

  • A new slice with elements in reversed order

func Shift

func Shift[T any](in []T) (T, []T)

Shift removes the first element from a slice and returns that element and the modified slice. Similar to JavaScript's Array.shift().

Parameters:

  • in: The input slice

Returns:

  • The removed element and the modified slice

func Slice

func Slice[T any](in []T, start, end int) []T

Slice returns a shallow copy of a portion of a slice into a new slice object. Similar to JavaScript's Array.slice().

Parameters:

  • in: The input slice
  • start: Zero-based index at which to start extraction (inclusive) If negative, it is treated as an offset from the end of the slice
  • end: Zero-based index at which to end extraction (exclusive) If negative, it is treated as an offset from the end of the slice

Returns:

  • A new slice containing the extracted elements

func Some

func Some[T any](in []T, pred func(T) bool) bool

Some tests whether at least one element in the slice passes the test implemented by the provided function. Similar to JavaScript's Array.some().

Parameters:

  • in: The input slice to test
  • pred: A function that tests each element and returns a boolean

Returns:

  • true if any element passes the test; otherwise, false

func Sort

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

Sort sorts the elements of a slice in place. Similar to JavaScript's Array.sort().

Parameters:

  • in: The input slice to sort
  • less: A function that returns true if element a should come before element b

Returns:

  • None (modifies the slice in-place)

func Splice

func Splice[T any](in []T, start, deleteCount int, items ...T) []T

Splice removes a specified number of elements from a slice starting at a given index and inserts new elements in their place. Similar to JavaScript's Array.splice().

Parameters:

  • in: The input slice from which elements will be removed and new elements will be inserted.
  • start: The index at which to start removing elements. If negative, it is treated as an offset from the end of the slice.
  • deleteCount: The number of elements to remove from the slice. If negative, it is treated as zero, meaning no elements will be removed.
  • items: A variadic parameter that accepts new elements to be inserted into the slice at the specified index.

Returns:

  • A new slice containing the elements from the original slice with the specified elements removed and new elements inserted.
  • If `start` is greater than the length of the slice, it will behave as if `start` is equal to the length of the slice, meaning no elements will be removed.

func ToReversed

func ToReversed[T any](in []T) []T

ToReversed returns a new slice with the elements in reversed order. Unlike Reverse, this method does not mutate the original slice. Similar to JavaScript's Array.toReversed().

Parameters:

  • in: The input slice to reverse

Returns:

  • A new slice with elements in reversed order

func ToString

func ToString[T any](in []T) string

ToString returns a string representing the specified slice and its elements. Similar to JavaScript's Array.toString().

Parameters:

  • in: The input slice to convert to a string

Returns:

  • A string representation of the slice

func Unshift

func Unshift[T any](in []T, item T) []T

Unshift adds one element to the beginning of a slice and returns the new slice. Similar to JavaScript's Array.unshift().

Parameters:

  • in: The input slice
  • item: The element to add to the beginning of the slice

Returns:

  • A new slice with the element added at the beginning

func Values

func Values[K comparable, V any](m map[K]V) []V

Values returns a slice containing the values of the map. Similar to JavaScript's Object.values().

Parameters:

  • m: The map from which to extract values

Returns:

  • A slice containing all the values from the map

func With

func With[T any](in []T, idx int, val T) []T

With returns a new slice with the element at the specified index replaced with the provided value. Similar to JavaScript's Array.with().

Parameters:

  • in: The input slice
  • idx: The index of the element to replace
  • val: The new value to replace with

Returns:

  • A new slice with the element at the specified index replaced

Panics:

  • If the index is out of range

Types

This section is empty.

Jump to

Keyboard shortcuts

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