new mapper helpers

This commit is contained in:
Hayden 2023-03-04 22:20:23 -09:00
parent 9f59a5d393
commit 4d744c0e4b
No known key found for this signature in database
GPG key ID: 17CF79474E257545

View file

@ -0,0 +1,32 @@
package repo
type MapFunc[T any, U any] func(T) U
func (a MapFunc[T, U]) Map(v T) U {
return a(v)
}
func (a MapFunc[T, U]) MapEach(v []T) []U {
result := make([]U, len(v))
for i, item := range v {
result[i] = a(item)
}
return result
}
func (a MapFunc[T, U]) MapErr(v T, err error) (U, error) {
if err != nil {
var zero U
return zero, err
}
return a(v), nil
}
func (a MapFunc[T, U]) MapEachErr(v []T, err error) ([]U, error) {
if err != nil {
return nil, err
}
return a.MapEach(v), nil
}