initial commit
This commit is contained in:
13
backend/pkg/ptr/ptr.go
Normal file
13
backend/pkg/ptr/ptr.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package ptr
|
||||
|
||||
func DerefOrZero[T any](ptrVal *T) T {
|
||||
var zeroT T
|
||||
if ptrVal == nil {
|
||||
return zeroT
|
||||
}
|
||||
return *ptrVal
|
||||
}
|
||||
|
||||
func Of[T any](v T) *T {
|
||||
return &v
|
||||
}
|
||||
36
backend/pkg/slices/map.go
Normal file
36
backend/pkg/slices/map.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package slices
|
||||
|
||||
// Map consumes []T and a function that returns D
|
||||
func Map[T, D any](srcItems []T, mapFunc func(T) D) []D {
|
||||
dstItems := make([]D, len(srcItems))
|
||||
for i, v := range srcItems {
|
||||
dstItems[i] = mapFunc(v)
|
||||
}
|
||||
return dstItems
|
||||
}
|
||||
|
||||
// First returns the first of a slice
|
||||
func First[T any](s []T) (item T, found bool) {
|
||||
if len(s) > 0 {
|
||||
return s[0], true
|
||||
}
|
||||
return item, false
|
||||
}
|
||||
|
||||
// Last returns the last of a slice
|
||||
func Last[T any](s []T) (item T, found bool) {
|
||||
if len(s) > 0 {
|
||||
return s[len(s)-1], true
|
||||
}
|
||||
return item, false
|
||||
}
|
||||
|
||||
// FindFirst finds the first matching item in s given fn
|
||||
func FindFirst[T any](s []T, fn func(T) bool) (item T, found bool) {
|
||||
for _, v := range s {
|
||||
if fn(v) {
|
||||
return v, true
|
||||
}
|
||||
}
|
||||
return item, false
|
||||
}
|
||||
24
backend/pkg/values/values.go
Normal file
24
backend/pkg/values/values.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package values
|
||||
|
||||
// FirstNonZero will return the first non zero value. If none exists,
|
||||
// it returns the zero value.
|
||||
func FirstNonZero[T comparable](vals ...T) T {
|
||||
var zeroT T
|
||||
for _, v := range vals {
|
||||
if v != zeroT {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return zeroT
|
||||
}
|
||||
|
||||
// CountNonZero returns the count of items that are non zero
|
||||
func CountNonZero[T comparable](vals ...T) (count int) {
|
||||
var zeroT T
|
||||
for _, v := range vals {
|
||||
if v != zeroT {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
Reference in New Issue
Block a user