Functions for slices using Go generics and (sometimes) specification pattern.
See full documentation here.
$ go get github.com/romain-jeannoutot/go-slices
Examples are based on example types, variables and functions definition.
func Every[T any](items []T, specification Specification) bool
The Every()
func returns true
if all items satisfy specification. Else, return false
.
Example :
goslices.Every(users, NewHasFirstnameSpecification()) // true
goslices.Every(users, NewFirstnameSpecification("John")) // false
func Filter[T any](items []T, specification Specification) []T
The Filter()
func returns a new slice with items satisfying specification.
Example :
goslices.Filter(users, NewFirstnameSpecification("John")) // John Doe, John Travis
goslices.Filter(users, NewFirstnameSpecification("Mike")) // []
func Find[T any](items []T, specification Specification) T
The Find()
func returns the first item in slice satisfying specification. Else, return empty value.
Example :
goslices.Find(users, NewFirstnameSpecification("John")) // John Doe
goslices.Find(users, NewFirstnameSpecification("Mike")) // Empty User
func FindIndex[T any](items []T, specification Specification) int
The FindIndex()
func returns index of the first item in slice satisfying specification. Else, return -1.
Example :
goslices.FindIndex(users, NewFirstnameSpecification("John")) // 0
goslices.FindIndex(users, NewFirstnameSpecification("Mike")) // -1
func Includes[T comparable](items []T, searchElement T) bool
The Includes()
func returns true
if at least one item in slice is equal to searchElement
. Else, return false
.
Example :
goslices.Includes(jobs, "developer") // true
goslices.Includes(jobs, "postman") // false
func Map[T any, S any](items []T, callback func(item T, idx int, items []T) S) []S
The Map()
func creates a new slice populated with elements returned by function called on each slice element.
Example :
goslices.Map(users, func(user User, _ int, _ []User) Employee {
return NewEmployee(user.firstname, user.lastname, goslices.Rand(jobs))
}) // []Employee
func Rand[T any](items []T) T
The Rand()
func returns a random element from slice. Empty value if slice is empty.
Example :
goslices.Rand(jobs) // string
func Reduce[T any, S any](items []T, callback func(acc S, item T, idx int, items []T) S, initialValue S) S
The Reduce()
func executes a reducer function on each slice element, passing in the return value from the calculation on the preceding element.
Example :
goslices.Reduce(users, func(acc map[string]int, user User, _ int, _ []User) map[string]int {
acc[user.firstname]++
return acc
}, map[string]int{}) // map[Aliyah:1 Arnas:1 John:2 Marcos:1]
func Some[T any](items []T, specification Specification) bool
The Some()
func returns true
if at least one item satisfy specification. Else, return false
.
Example :
goslices.Some(users, NewFirstnameSpecification("John")) // true
goslices.Some(users, NewFirstnameSpecification("Mike")) // false
This package is licensed under MIT license. See LICENSE for details.