Skip to content

Commit 4d9acd2

Browse files
committed
implemented fistNonZero
1 parent 2399641 commit 4d9acd2

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

slice/sliceutil.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,17 @@ func Merge[V comparable](ss ...[]V) []V {
160160
func MergeItems[V comparable](items ...V) []V {
161161
return Dedupe(items)
162162
}
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

Comments
 (0)