github.com/searKing/golang/go@v1.2.74/container/slice/findfirst.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  // FindFirstFunc returns an {@link Optional} describing the first element of this stream,
     8  // or an empty {@code Optional} if the stream is empty.  If the stream has
     9  // no encounter order, then any element may be returned.
    10  func FindFirstFunc(s interface{}, f func(interface{}) bool) interface{} {
    11  	return normalizeElem(findFirstFunc(Of(s), f, true), s)
    12  }
    13  
    14  // findFirstFunc is the same as FindFirstFunc.
    15  func findFirstFunc(s []interface{}, f func(interface{}) bool, truth bool) interface{} {
    16  	idx := findFirstIndexFunc(s, f, truth)
    17  	if idx == -1 {
    18  		return nil
    19  	}
    20  	return s[idx]
    21  }