Documentation
¶
Index ¶
- func MapToSlice[K comparable, V, T any](elements map[K]V, transform func(K, V) T) []T
- func Marshal(data any) (string, error)
- func MarshalSafe(data any) string
- func SliceAddOrUpdate[T any](elements []T, newItem T, predicate func(T) bool) []T
- func SliceDistinct[T any, K comparable](elements []T, keySelector func(item T) K) []T
- func SliceFilter[T any](elements []T, predicate func(T) bool) []T
- func SliceFilterAndMap[T any, V any](elements []T, transform func(T) (bool, V)) []V
- func SliceFind[T any](elements []T, predicate func(T) bool) T
- func SliceFindLast[T any](elements []T, predicate func(T) bool) T
- func SliceGroupBy[T any, K comparable](elements []T, keySelector func(item T) K) map[K][]T
- func SliceMap[T any, V any](elements []T, transform func(T) V) []V
- func SliceMapErr[T any, V any](elements []T, transform func(T) (V, error)) ([]V, error)
- func SliceShuffle[T any](elements []T) []T
- func SliceToMap[T any, K comparable](elements []T, keySelector func(item T) K) map[K]T
- func Unmarshal[T any](data string) (T, error)
- func UnmarshalSafe[T any](data string) T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MapToSlice ¶
func MapToSlice[K comparable, V, T any](elements map[K]V, transform func(K, V) T) []T
MapToSlice converts a map to a slice
Parameters:
- elements - The map to convert
- transform - The function to apply to each element
Returns:
- []T - The slice of transformed elements
Example:
map := map[string]int{"a": 1, "b": 2, "c": 3}
slice := MapToSlice(map, func(k string, v int) int {
return v * 2
})
// slice := []int{2, 4, 6}
func Marshal ¶
Marshal encodes a Go value into a string according to the provided marshal and encode options.
Parameters:
- data: T
Returns:
- string: string
- error: error
func MarshalSafe ¶
MarshalSafe encodes a Go value into a string according to the provided marshal and encode options.
Parameters:
- data: T
Returns:
- string: string
func SliceAddOrUpdate ¶
SliceAddOrUpdate checks if an element matching the predicate exists in the slice. If a match is found, the existing element is replaced with newItem (Update) in a new copy. If no match is found, newItem is appended to the slice (Add).
Parameters:
- elements: The input slice to process ([]T).
- newItem: The item to add or use for the update (T).
- predicate: A function that takes an element of type T and returns a bool. The first element that evaluates to true is the one to be updated.
Returns:
- []T: A new slice containing the updated or added element.
Example:
users := []User{{ID: 1, Name: "Alice"}, {ID: 2, Name: "Bob"}}
updatedUsers := SliceAddOrUpdate(users, User{ID: 1, Name: "Alyssa"}, func(u User) bool { return u.ID == 1 })
// updatedUsers == []User{{ID: 1, Name: "Alyssa"}, {ID: 2, Name: "Bob"}}
addedUsers := SliceAddOrUpdate(updatedUsers, User{ID: 3, Name: "Charlie"}, func(u User) bool { return u.ID == 3 })
// addedUsers == []User{{ID: 1, Name: "Alyssa"}, {ID: 2, Name: "Bob"}, {ID: 3, Name: "Charlie"}}
func SliceDistinct ¶
func SliceDistinct[T any, K comparable](elements []T, keySelector func(item T) K) []T
SliceDistinct returns a new slice containing only the unique elements from the input slice.
The function uses a key selector function to determine uniqueness. Elements are considered distinct if they produce different keys when passed to the selector function.
Parameters:
- elements - the input slice to process ([]T)
- keySelector - function that extracts a comparable key from each element (func(T) K)
Returns:
- []T - a new slice containing only the distinct elements from the input
Example:
// Get distinct users by ID
users := []User{
{ID: 1, Name: "Alice"},
{ID: 2, Name: "Bob"},
{ID: 1, Name: "Alice"},
}
distinctUsers := SliceDistinct(users, func(u User) int { return u.ID })
// distinctUsers == []User{
// {ID: 1, Name: "Alice"},
// {ID: 2, Name: "Bob"},
// }
func SliceFilter ¶
SliceFilter iterates over a slice and returns a new slice containing all elements that satisfy the given predicate function.
Parameters:
- elements: The input slice to filter (type []T where T is any type)
- predicate: A function that takes an element of type T and returns a bool. Elements that evaluate to true are included in the result.
Returns:
- A new slice containing only elements that satisfy the predicate.
Type Parameters:
- T: The type of elements in the slice (can be any type)
Example:
// Filter even numbers
numbers := []int{1, 2, 3, 4, 5}
evens := SliceFilter(numbers, func(n int) bool { return n%2 == 0 })
// evens == []int{2, 4}
func SliceFilterAndMap ¶
SliceFilterAndMap applies a transformation function to each element of a slice and returns a new slice that contains the transformed elements.
Parameters:
- elements: The input slice to process ([]T)
- transform: function that transforms each element of the slice (func(T) (V, error))
Returns:
- []V: a slice of transformed elements
- error: an error if any occurred during processing
Example:
// Filter out even numbers
numbers := []int{1, 2, 3}
numbersByEvenOdd := SliceFilterAndMap(numbers, func(n int) (bool, string) {
return n%2 == 0, fmt.Sprintf("Number %d is even", n)
})
// numbersByEvenOdd == []string{"Number 2 is even"}
func SliceFind ¶
SliceFind returns the first element in a slice that satisfies the given predicate function. If no element is found, it returns the zero value of type T.
Parameters:
- elements: The input slice to search (type []T where T is any type)
- predicate: A function that takes an element of type T and returns a bool. The first element that evaluates to true is returned.
Returns:
- The first matching element, or zero value if none found.
Type Parameters:
- T: The type of elements in the slice (can be any type)
Example:
// Find first even number
numbers := []int{1, 3, 2, 4}
firstEven := SliceFind(numbers, func(n int) bool { return n%2 == 0 })
// firstEven == 2
func SliceFindLast ¶
SliceFindLast returns the last element in a slice that satisfies the given predicate function. If no element is found, it returns the zero value of type T.
Parameters:
- elements: The input slice to search (type []T where T is any type)
- predicate: A function that takes an element of type T and returns a bool. The last element that evaluates to true is returned.
Returns:
- The last matching element, or zero value if none found.
Type Parameters:
- T: The type of elements in the slice (can be any type)
Example:
// Find last even number
numbers := []int{1, 2, 3, 4, 5}
lastEven := SliceFindLast(numbers, func(n int) bool { return n%2 == 0 })
// lastEven == 4
func SliceGroupBy ¶
func SliceGroupBy[T any, K comparable](elements []T, keySelector func(item T) K) map[K][]T
SliceGroupBy groups the elements of a slice by a key selector function.
Parameters:
- elements: the input slice to process ([]T)
- keySelector: function that extracts a comparable key from each element (func(T) K)
Returns:
- map[K][]T: a map where the keys are the result of the key selector function, and the values are slices of elements that share the same key
Example:
// Group users by age
users := []User{
{ID: 1, Name: "Alice", Age: 30},
{ID: 2, Name: "Bob", Age: 25},
{ID: 3, Name: "Charlie", Age: 30},
}
groupedUsers := SliceGroupBy(users, func(u User) int { return u.Age })
// groupedUsers == map[int][]User{
// 25: []User{{ID: 2, Name: "Bob", Age: 25}},
// 30: []User{{ID: 1, Name: "Alice", Age: 30}, {ID: 3, Name: "Charlie", Age: 30}},
// }
func SliceMap ¶
SliceMap applies a transformation function to each element of a slice and returns a new slice containing the transformed values. This is a functional programming style operation commonly known as "map" or "transform".
Parameters:
- elements: The input slice to transform (type []T where T is any type)
- transform: A function that takes an element of type T and returns a value of type V. This function is applied to each element in the slice.
Returns:
- A new slice of type []V containing the transformed values.
Type Parameters:
- T: The type of elements in the input slice
- V: The type of elements in the output slice
Example:
// Convert strings to their lengths
names := []string{"Alice", "Bob", "Charlie"}
lengths := SliceMap(names, func(s string) int { return len(s) })
// lengths == []int{5, 3, 7}
func SliceMapErr ¶
SliceMapErr applies a transformation function to each element of a slice, returning a new slice containing the transformed values. If the transformation function returns an error for any element, processing stops immediately, and that error is returned.
Parameters:
- elements: The input slice to transform ([]T).
- transform: A function that converts an element of type T to a value of type V or returns an error (func(T) (V, error)).
Returns:
- []V: A new slice of transformed elements.
- error: The first error encountered during the transformation, or nil.
func SliceShuffle ¶
func SliceShuffle[T any](elements []T) []T
SliceShuffle randomly shuffles the elements of the given slice and returns it.
The function modifies the order of elements in place using the Fisher–Yates shuffle algorithm provided by Go's standard library (`rand.Shuffle`). This ensures that each permutation of the slice has an equal probability of occurrence.
Parameters:
- elements - the input slice to shuffle ([]T)
Returns:
- []T - the same slice with its elements randomly shuffled
Example:
// Shuffle a list of numbers
numbers := []int{1, 2, 3, 4, 5}
shuffled := SliceShuffle(numbers)
// shuffled might be [3, 5, 1, 4, 2]
func SliceToMap ¶
func SliceToMap[T any, K comparable](elements []T, keySelector func(item T) K) map[K]T
SliceToMap converts a slice of elements into a map using a key selector function.
Parameters:
- elements - the input slice to convert ([]T)
- keySelector - function that extracts a key from each element (func(T) K)
Returns:
- map[K]T - a map where keys are produced by the keySelector function and values are the original elements
Example:
// Convert a list of users to a map by their IDs
users := []User{
{ID: 1, Name: "Alice"},
{ID: 2, Name: "Bob"},
}
usersMap := SliceToMap(users, func(u User) int { return u.ID })
// usersMap == map[int]User{
// 1: {ID: 1, Name: "Alice"},
// 2: {ID: 2, Name: "Bob"},
// }
func Unmarshal ¶
Unmarshal decodes a string input into a Go value according to the provided unmarshal and decode options.
Parameters:
- data: string
Returns:
- T: T
- error: error
func UnmarshalSafe ¶
UnmarshalSafe is a safe version of json.Unmarshal
Parameters:
- data: string
Returns:
- T: T
Types ¶
This section is empty.