Documentation
¶
Index ¶
- func Chunk[T any](elements []T, chunkSize int) [][]T
- func Contains[T comparable](slice []T, value T) bool
- func Filter[T any](vs []T, f func(T) bool) []T
- func FilterMap[From any, To any](vs []From, f func(From) (To, bool)) []To
- func Ident[T any](t T) T
- func Intersects[T comparable](slice []T, values []T) bool
- func Map[From any, To any](items []From, mapper func(in From) To) []To
- func Of[T any](items ...T) []T
- func OfStringers(items ...fmt.Stringer) []string
- func ToMap[T any, K comparable, V any](slice []T, fn func(idx int, elem T) (K, V)) map[K]V
- func TryMap[From any, To any](items []From, mapper func(in From) (*To, error)) ([]To, error)
- func Unique[T comparable](vs []T) []T
- func UniqueBy[T any, V comparable](vs []T, selector func(v T) V) []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chunk ¶ added in v0.4.3
Chunk divides a slice of elements into multiple smaller slices, each of a specified size.
The function takes a generic type parameter `T` which allows it to work with slices of any type.
Parameters:
- elements: A slice of elements of type `T` to be divided into chunks.
- chunkSize: An integer representing the size of each chunk. If chunkSize is less than or equal to 0, an empty slice is returned.
Returns:
- A two-dimensional slice of type `T` where each inner slice is of length `chunkSize` (except possibly the last one). If chunkSize is less than or equal to 0, an empty slice of slices is returned. If the input slice is empty, an empty slice of slices is returned.
Example usage:
chunks := Chunk([]int{1, 2, 3, 4, 5, 6}, 2) // chunks will be [][]int{{1, 2}, {3, 4}, {5, 6}} chunks = Chunk([]int{1, 2, 3, 4, 5, 6}, 3) // chunks will be [][]int{{1, 2, 3}, {4, 5, 6}} chunks = Chunk([]int{1, 2, 3, 4, 5, 6}, 0) // chunks will be [][]int{} chunks = Chunk([]int{}, 3) // chunks will be [][]int{}
func Contains ¶
func Contains[T comparable](slice []T, value T) bool
Contains checks if a given value exists within a slice. The function is generic and can operate on slices of any type that supports comparison.
Parameters:
- slice: The slice of elements to be searched.
- value: The value to search for within the slice.
Returns:
- bool: Returns true if the value is found within the slice, otherwise returns false.
Example usage:
- Contains([]int{1, 2, 3, 4}, 3) // returns true
- Contains([]string{"apple", "banana", "cherry"}, "banana") // returns true
- Contains([]string{"apple", "banana", "cherry"}, "date") // returns false
Type constraints:
- T: The type of elements in the slice, which must be comparable.
func Filter ¶
Filter returns a new slice containing elements from the input slice that satisfy the given predicate function. The function checks each element in the input slice and appends it to the output slice if the predicate returns true for that element. The input slice remains unchanged.
The type parameter T represents the type of elements in the input and output slices.
func FilterMap ¶
FilterMap filters and transforms a slice of elements of type `From` to a slice of elements of type `To`. The transformation and filtering are specified by the provided function `f`.
The function `f` takes an element of type `From` and returns a tuple `(To, bool)`. If the boolean value is `true`, the transformed value is included in the result slice. If the boolean value is `false`, the transformed value is excluded from the result slice.
BlockType Parameters: - From: the type of the elements in the input slice. - To: the type of the elements in the output slice.
Parameters: - vs: the input slice containing elements of type `From`. - f: the transformation and filtering function.
Returns: - A slice of elements of type `To` that satisfies the filter condition.
Example:
func isNonEmptyString(s string) (string, bool) { if s != "" { return s, true } return "", false }
input := []string{"a", "", "b", "c", ""} result := FilterMap(input, isNonEmptyString) // result: []string{"a", "b", "c"}
func Ident ¶
func Ident[T any](t T) T
Ident returns the input value without modifications. It is a generic identity function that works for any type.
func Intersects ¶ added in v0.6.0
func Intersects[T comparable](slice []T, values []T) bool
Intersects determines if there is any intersection between two slices. The function is generic and can operate on slices of any type that supports comparison.
Parameters:
- slice: The first slice of elements.
- values: The second slice of elements to be compared against the first slice.
Returns:
- bool: Returns true if any element in the `values` slice exists within the `slice`, otherwise returns false.
Example usage:
- Intersects([]int{1, 2, 3, 4}, []int{3, 5}) // returns true
- Intersects([]string{"apple", "banana", "cherry"}, []string{"banana", "date"}) // returns true
- Intersects([]string{"apple", "banana", "cherry"}, []string{"date", "fig"}) // returns false
Type constraints:
- T: The type of elements in the slices, which must be comparable.
func Map ¶
Map applies a given mapper function to all elements of the input slice and returns a new slice containing the results.
Parameters:
- items: A slice of elements of type From, representing the input elements.
- mapper: A function that takes an element of type From and returns an element of type To, representing the transformation to be applied to each element of the input slice.
Returns:
- A new slice of elements of type To, containing the results of applying the mapper function to each element of the input slice.
Example:
transformed := Map([]int{1, 2, 3}, func(in int) string { return fmt.Sprintf("Number %d", in) }) // transformed would be []string{"Number 1", "Number 2", "Number 3"}
func Of ¶ added in v0.3.0
func Of[T any](items ...T) []T
Of takes a variadic number of items of any type and returns them as a slice.
func OfStringers ¶ added in v0.3.0
OfStringers converts a variadic list of fmt.Stringer items to a slice of strings by calling String() on each item.
func ToMap ¶
func ToMap[T any, K comparable, V any]( slice []T, fn func(idx int, elem T) (K, V), ) map[K]V
ToMap transforms a slice into a map by applying a provided transformation function to each element.
The transformation function `fn` is called for each element in the `slice`, with its index and value. It returns a key-value pair that will be used to populate the resulting map.
The generic type parameters are: - `T`: The type of elements in the input slice. - `K`: The type of keys in the resulting map. Must be comparable. - `V`: The type of values in the resulting map.
Parameters: - `slice`: A slice of elements to transform. - `fn`: A function that takes an element's index and the element itself, and returns a key-value pair.
Returns: A map of type `map[K]V` where each key-value pair is obtained by applying `fn` to each element of `slice`.
Example:
// Example of transforming a slice of strings into a map where the key is the string and the value is its length: words := []string{"apple", "banana", "cherry"} result := ToMap(words, func(idx int, elem string) (string, int) { return elem, len(elem) }) // result is map[string]int{"apple": 5, "banana": 6, "cherry": 6}
func TryMap ¶
TryMap applies a given mapper function to all elements of the input slice and returns a new slice containing the results. Similar to Map function, TryMap allows the mapper function to return a pointer to the transformed element along with an error. If any error occurs during the mapping process, TryMap stops and returns the current result along with the error.
Parameters:
- items: A slice of elements of type From, representing the input elements.
- mapper: A function that takes an element of type From and returns a pointer to an element of type To, along with an error. This function represents the transformation to be applied to each element of the input slice.
Returns:
- A new slice of elements of type To, containing the results of applying the mapper function to each element of the input slice.
- If any error occurs during the mapping process, TryMap returns the current result along with the error.
Example:
transformed, err := TryMap([]int{1, 2, 3}, func(in int) (*string, error) { str := fmt.Sprintf("Number %d", in) return &str, nil }) // transformed would be []string{"Number 1", "Number 2", "Number 3"}, and err would be nil
func Unique ¶
func Unique[T comparable](vs []T) []T
Unique returns a new slice that contains only the unique elements from the input slice `vs`. It uses the Ident function as the selector to determine uniqueness.
func UniqueBy ¶
func UniqueBy[T any, V comparable](vs []T, selector func(v T) V) []T
UniqueBy returns a new slice that contains only the unique elements from the input slice `vs` based on a specified key selected by the `selector` function. It uses the provided `selector` function to determine the key for comparison.
This function leverages the `Filter` function to iterate through the input slice `vs` and include elements in the output slice only if their selector key is unique within the slice.
BlockType Parameters:
- T: The type of elements in the input and output slices.
- V: The type of the key returned by the `selector` function for comparison; it must be comparable.
Parameters:
- vs: A slice of elements of type T to be filtered for uniqueness.
- selector: A function that takes an element of type T and returns a value of type V, which is used for determining uniqueness.
Returns:
A new slice containing only the unique elements from the input slice `vs`, based on the keys provided by the `selector` function.
Example usage:
items := []Item{{ID: 1}, {ID: 2}, {ID: 1}} uniqueItems := UniqueBy(items, func(i Item) int { return i.ID }) // uniqueItems will contain [{ID: 1}, {ID: 2}]
Types ¶
This section is empty.