dynamo-browse/internal/common/sliceutils/map.go
Leon Mika 4b4d515ade
Added a few changes to query expressions (#51)
- Added the between operator to query expressions.
- Added the using query expression suffix to specify which index to query (or force a scan). This is required if query planning has found multiple indices that can potentially be used.
- Rewrote the types of the query expressions to allow for functions to be defined once, and be useful in queries that result in DynamoDB queries, and evaluation.
- Added some test functions around time and summing numbers.
- Fixed a bug in the del-attr which was not honouring marked rows in a similar way to set-attr: it was only deleting attributes from the first row.
- Added the -to type flag to set-attr which will set the attribute to the value of a query expression.
2023-04-14 15:35:43 +10:00

61 lines
1 KiB
Go

package sliceutils
func All[T any](ts []T, predicate func(t T) bool) bool {
for _, t := range ts {
if !predicate(t) {
return false
}
}
return true
}
func Map[T, U any](ts []T, fn func(t T) U) []U {
us := make([]U, len(ts))
for i, t := range ts {
us[i] = fn(t)
}
return us
}
func MapWithError[T, U any](ts []T, fn func(t T) (U, error)) ([]U, error) {
us := make([]U, len(ts))
for i, t := range ts {
var err error
us[i], err = fn(t)
if err != nil {
return nil, err
}
}
return us, nil
}
func Filter[T any](ts []T, fn func(t T) bool) []T {
us := make([]T, 0)
for _, t := range ts {
if fn(t) {
us = append(us, t)
}
}
return us
}
func FindFirst[T any](ts []T, fn func(t T) bool) (returnedT T, found bool) {
for _, t := range ts {
if fn(t) {
return t, true
}
}
return returnedT, false
}
func FindLast[T any](ts []T, fn func(t T) bool) (returnedT T, found bool) {
for i := len(ts) - 1; i >= 0; i-- {
t := ts[i]
if fn(t) {
return t, true
}
}
return returnedT, false
}