github.com/searKing/golang/go@v1.2.74/container/slice/min.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  // MinFunc returns the minimum element of this stream according to the provided.
    12  func MinFunc(s interface{}, f func(interface{}, interface{}) int) interface{} {
    13  	return normalizeElem(minFunc(Of(s), f), s)
    14  
    15  }
    16  
    17  // minFunc is the same as MinFunc
    18  func minFunc(s []interface{}, f func(interface{}, interface{}) int) interface{} {
    19  	object.RequireNonNil(s, "minFunc called on nil slice")
    20  	object.RequireNonNil(f, "minFunc called on nil callfn")
    21  
    22  	return ReduceFunc(s, func(left, right interface{}) interface{} {
    23  		if f(left, right) < 0 {
    24  			return left
    25  		}
    26  		return right
    27  	})
    28  }