variadic max and inrange functions

This commit is contained in:
John O'Keefe 2023-11-04 13:08:46 -04:00
parent 5f11ccf9f6
commit 66f06e8dd9

33
playground/variadic.go Normal file
View File

@ -0,0 +1,33 @@
package main
import (
"fmt"
"math"
)
func maximum(numbers ...float64) float64 {
max := math.Inf(-1)
for _, number := range numbers {
if number > max {
max = number
}
}
return max
}
func inRange(min float64, max float64, numbers ...float64) []float64 {
var result []float64
for _, number := range numbers {
if number >= min && number <= max {
result = append(result, number)
}
}
return result
}
func variadic() {
fmt.Println(maximum(71.8, 56.2, 89.5))
fmt.Println(maximum(90.7, 89.7, 98.5, 92.3))
fmt.Println(inRange(1, 100, -12.5, 3.2, 0, 50, 103.5))
fmt.Println(inRange(-10, 10, 4.1, 12, -12, -5.2))
}