Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
issue51925.go52 linesDownload Raw Back to typeparam
1// compile2 3// Copyright 2022 The Go Authors. All rights reserved.4// Use of this source code is governed by a BSD-style5// license that can be found in the LICENSE file.6 7package main8 9import "fmt"10 11type IntLike interface {12	~int | ~int64 | ~int32 | ~int16 | ~int813}14 15func Reduce[T any, U any, Uslice ~[]U](function func(T, U) T, sequence Uslice, initial T) T {16	result := initial17	for _, x := range sequence {18		result = function(result, x)19	}20	return result21}22 23func min[T IntLike](x, y T) T {24	if x < y {25		return x26	}27	return y28}29 30// Min returns the minimum element of `nums`.31func Min[T IntLike, NumSlice ~[]T](nums NumSlice) T {32	if len(nums) == 0 {33		return T(0)34	}35	return Reduce(min[T], nums, nums[0])36}37 38// VarMin is the variadic version of Min.39func VarMin[T IntLike](nums ...T) T {40	return Min(nums)41}42 43type myInt int44 45func main() {46	fmt.Println(VarMin(myInt(1), myInt(2)))47 48	seq := []myInt{1, 2}49	fmt.Println(Min(seq))50	fmt.Println(VarMin(seq...))51}52 
codekingpro/portable-devtools · Team Ai