Functions for slices using Go generics and (sometimes) specification pattern.
See full documentation here.
$ go get github.com/romain-jeannoutot/go-slicesExamples are based on example types, variables and functions definition.
func Every[T any](items []T, specification Specification) boolThe Every() func returns true if all items satisfy specification. Else, return false.
Example :
goslices.Every(users, NewHasFirstnameSpecification()) // true
goslices.Every(users, NewFirstnameSpecification("John")) // falsefunc Filter[T any](items []T, specification Specification) []TThe 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) TThe 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 Userfunc FindIndex[T any](items []T, specification Specification) intThe 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")) // -1func Includes[T comparable](items []T, searchElement T) boolThe 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") // falsefunc Map[T any, S any](items []T, callback func(item T, idx int, items []T) S) []SThe 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))
}) // []Employeefunc Rand[T any](items []T) TThe Rand() func returns a random element from slice. Empty value if slice is empty.
Example :
goslices.Rand(jobs) // stringfunc Reduce[T any, S any](items []T, callback func(acc S, item T, idx int, items []T) S, initialValue S) SThe 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) boolThe 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")) // falseThis package is licensed under MIT license. See LICENSE for details.