Package: arr
Utility functions for arrays, slices, and maps in Go: filter, map, reverse, shuffle, chunk, merge, keys, values, min/max, sum, unique, and more.
Repository: https://github.com/dracory/arr
Installation
go get github.com/dracory/arr
Features
- Array Manipulation
- Filter, Map, Reverse, Shuffle
- Split into chunks, Merge arrays
- Remove/Move elements by index
- Array Analysis
- Min, Max, Sum
- Count, Count by condition
- Contains/ContainsAll, IndexOf
- Unique values
- Map Operations
- Keys, Values
- GroupBy
- Map equality
- Iteration
- Iterate with callbacks
- Filter empty values
Usage Examples
Filtering Arrays
package main
import (
"fmt"
"github.com/dracory/arr"
)
func main() {
numbers := []int{1,2,3,4,5,6,7,8,9,10}
even := arr.Filter(numbers, func(n int) bool { return n%2==0 })
fmt.Println(even) // [2 4 6 8 10]
}
Mapping Arrays
package main
import (
"fmt"
"github.com/dracory/arr"
)
func main() {
numbers := []int{1,2,3,4,5}
doubled := arr.Map(numbers, func(n int) int { return n*2 })
fmt.Println(doubled) // [2 4 6 8 10]
}
Array Manipulation
package main
import (
"fmt"
"github.com/dracory/arr"
)
func main() {
numbers := []int{1,2,3,4,5}
// Reverse
fmt.Println(arr.Reverse(numbers)) // [5 4 3 2 1]
// Shuffle (random order)
fmt.Println(arr.Shuffle(numbers))
// Split into chunks
fmt.Println(arr.Split(numbers, 2)) // [[1 2] [3 4] [5]]
}
Map Operations
package main
import (
"fmt"
"github.com/dracory/arr"
)
func main() {
m := map[string]string{"name":"John","age":"30","email":"john@example.com"}
fmt.Println(arr.Keys(m)) // [name age email]
fmt.Println(arr.Values(m)) // [John 30 john@example.com]
}
Array Analysis
package main
import (
"fmt"
"github.com/dracory/arr"
)
func main() {
numbers := []int{5,2,9,1,7,3,8,4,6}
fmt.Println(arr.Min(numbers)) // 1
fmt.Println(arr.Max(numbers)) // 9
fmt.Println(arr.Sum(numbers)) // 45
fmt.Println(arr.Count(numbers)) // 9
dup := []int{1,2,2,3,3,3,4,4,4,4}
fmt.Println(arr.Unique(dup)) // [1 2 3 4]
}
Notes
- Designed for convenience over micro-optimizations
- Some operations may mutate input slices (e.g., Shuffle, Reverse)
- For very large datasets, consider tailored stdlib approaches for performance