The Slice Utility Project provides a comprehensive set of utility functions to manipulate slices in Go. These utilities are designed to simplify common operations such as filtering, mapping, and reducing slices, thereby enhancing productivity and code readability.
- String: Convert all elements to strings.
- Length: Return the length of the slice.
- Filter: Remove elements from a slice based on a condition.
- Map: Transform elements in a slice using a provided function.
- Reduce: Reduce a slice to a single value by applying a function cumulatively to the elements.
- Unique: Return a new slice with unique elements based on a key function.
- Concat: Concatenate multiple slices into one.
- CopyWithIn: Create a new slice containing elements at specified indices.
- Every: Check if all elements satisfy a given predicate.
- Find: Search for the first element that satisfies a given predicate.
- FindIndex: Find the index of the first element that satisfies a given predicate.
- FindLast: Search for the last element that satisfies a given predicate.
- FindLastIndex: Find the index of the last element that satisfies a given predicate.
- ForEach: Iterate over each element and apply a provided function.
- Join: Convert all elements to strings and join them with a specified separator.
- Slice: Return a subset of the slice.
- Fill: Set all elements of the slice to a specified value.
- At: Retrieve the element at a specified index.
- Sort: Sort the slice based on a comparison function.
- Values: Return the underlying slice of elements.
- Remove: Remove elements from a slice based on a condition.
- RemoveAt: Remove elements from a slice based on a condition.
- Reverse: Reverse the order of elements in the slice.
To install the Slice Utility Project, use the following command:
go get github.com/aide-cloud/slice@latest
To use the Slice Utility Project in your Go project, you can import it as follows:
import "github.com/aide-cloud/slice"
You can create an advanced slice with initial data or an empty advanced slice.
// Create an advanced slice with initial data
advanced := slice.NewAdvancedSlice([]int{1, 2, 3, 4})
// Create an empty advanced slice
emptyAdvanced := slice.NewAdvancedSlice[int](nil)
Transform elements in the slice using a provided function.
transformed := advanced.Map(func(item int, index int) int {
return item * 2
})
Remove elements from a slice based on a condition.
filtered := advanced.Filter(func(item int, index int)) bool {
return item > 2
})
Search for elements that satisfy a given predicate.
firstMatch := advanced.Find(func(item int) bool {
return item == 3
})
index := advanced.FindIndex(func(item int) bool {
return item == 3
})
Sort the slice based on a comparison function.
sorted := advanced.Sort(func(a int, b int) bool {
return a < b
})
Concatenate multiple slices into one.
concatenated := advanced.Concat(NewAdvancedSlice([]int{5, 6, 7}))
Return a new slice with unique elements based on a key function.
unique := advanced.Unique(func(item int) string {
return fmt.Sprintf("%d", item)
})
Iterate over each element and apply a provided function.
advanced.ForEach(func(item int, index int) {
fmt.Printf("Element %d: %d\n", index, item)
})
This README provides an overview of the Slice Utility Project along with examples of how to use its various functionalities. For more detailed information, please refer to the inline documentation within the source code.