12 lines
244 B
Go

package utils
// Let applies a function `f` to a pointer value `ptr` if it's not nil.
// Returns nil if the pointer is nil.
func Let[T any, R any](ptr *T, f func(T) R) *R {
if ptr == nil {
return nil
}
result := f(*ptr)
return &result
}