3 // Copyright 2022 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 type IntLike interface {
12 ~int | ~int64 | ~int32 | ~int16 | ~int8
15 func Reduce[T any, U any, Uslice ~[]U](function func(T, U) T, sequence Uslice, initial T) T {
17 for _, x := range sequence {
18 result = function(result, x)
23 func min[T IntLike](x, y T) T {
31 // Min returns the minimum element of `nums`.
32 func Min[T IntLike, NumSlice ~[]T](nums NumSlice) T {
36 return Reduce(min[T], nums, nums[0])
39 // VarMin is the variadic version of Min.
40 func VarMin[T IntLike](nums ...T) T {
47 fmt.Println(VarMin(myInt(1), myInt(2)))
51 fmt.Println(VarMin(seq...))