A collection of functional looking functions I kept rewriting in my Go projects.
func PassErr[T any](t T, err error) T
Ignores err
and returns t
.
func LogErr[T any](t T, err error) T
Logs err
with log.Println
if err
is non-nil
and returns t
.
func FatalErr[T any](t T, err error) T
Logs err
with log.Fatalln
if err
is non-nil
, otherwise returns t
.
func FatalErr[T any](t T, err error) T
Logs err
with log.Panicln
if err
is non-nil
, otherwise returns t
.
func Reduce[T any](f func(T, T) T, a []T) T
Applies f
cumulatively to the items in a
, left to right. Similarly to Haskell's foldl'
func Map[T any, V any](f func(T) V, a []T) []V
Maps f
over the items in a
one by one.
func MaptoStr[T any](a []T) []string
Equivalent to Map(fmt.Sprintf, a)
func Filter[T any](f func(T) bool, a []T) []T
Returns a slice of all items v
in a
for which f(v)
returns true
func Find[T any](f func(T) bool, a []T) (int, T)
Returns first index and item v
in a
for which f(v)
returns true