github.com/searKing/golang/go@v1.2.74/container/slice/findfirstIndex.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package slice
     6  
     7  import (
     8  	"github.com/searKing/golang/go/util/object"
     9  )
    10  
    11  // FindFirstFunc returns an {@link Optional} describing the first index of this stream,
    12  // or an empty {@code Optional} if the stream is empty.  If the stream has
    13  // no encounter order, then any element may be returned.
    14  func FindFirstIndexFunc(s interface{}, f func(interface{}) bool) int {
    15  	return findFirstIndexFunc(Of(s), f, true)
    16  }
    17  
    18  // findFirstFunc is the same as FindFirstFunc.
    19  func findFirstIndexFunc(s []interface{}, f func(interface{}) bool, truth bool) int {
    20  	object.RequireNonNil(s, "findFirstIndexFunc called on nil slice")
    21  	object.RequireNonNil(f, "findFirstIndexFunc called on nil callfn")
    22  
    23  	for idx, r := range s {
    24  		if f(r) == truth {
    25  			return idx
    26  		}
    27  	}
    28  	return -1
    29  }