We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2399641 commit 4d9acd2Copy full SHA for 4d9acd2
1 file changed
slice/sliceutil.go
@@ -160,3 +160,17 @@ func Merge[V comparable](ss ...[]V) []V {
160
func MergeItems[V comparable](items ...V) []V {
161
return Dedupe(items)
162
}
163
+
164
+// FirstNonZero function takes a slice of comparable type inputs, and returns
165
+// the first non-zero element in the slice along with a boolean value indicating
166
+// if a non-zero element was found or not.
167
+func FirstNonZero[T comparable](inputs []T) (T, bool) {
168
+ var zero T
169
+ for _, v := range inputs {
170
+ if v != zero {
171
+ return v, true
172
+ }
173
174
175
+ return zero, false
176
+}
0 commit comments