github.com/searKing/golang/go@v1.2.74/container/slice/max.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 // MaxFunc returns the maximum element of this stream according to the provided. 12 func MaxFunc(s interface{}, f func(interface{}, interface{}) int) interface{} { 13 return normalizeElem(minFunc(Of(s), f), s) 14 15 } 16 17 // maxFunc is the same as MaxFunc 18 func maxFunc(s []interface{}, f func(interface{}, interface{}) int) interface{} { 19 object.RequireNonNil(s, "maxFunc called on nil slice") 20 object.RequireNonNil(f, "maxFunc 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 }