github.com/blend/go-sdk@v1.20220411.3/mathutil/min_max.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package mathutil
     9  
    10  import "time"
    11  
    12  // MinMax returns both the min and max in one pass.
    13  func MinMax(values []float64) (min, max float64) {
    14  	if len(values) == 0 {
    15  		return
    16  	}
    17  	min = values[0]
    18  	max = values[0]
    19  	for _, v := range values {
    20  		if max < v {
    21  			max = v
    22  		}
    23  		if min > v {
    24  			min = v
    25  		}
    26  	}
    27  	return
    28  }
    29  
    30  // MinMaxInts returns both the min and max of ints in one pass.
    31  func MinMaxInts(values []int) (min, max int) {
    32  	if len(values) == 0 {
    33  		return
    34  	}
    35  	min = values[0]
    36  	max = values[0]
    37  	for _, v := range values {
    38  		if max < v {
    39  			max = v
    40  		}
    41  		if min > v {
    42  			min = v
    43  		}
    44  	}
    45  	return
    46  }
    47  
    48  // MinMaxDurations returns both the min and max of time.Duration in one pass.
    49  func MinMaxDurations(values []time.Duration) (min, max time.Duration) {
    50  	if len(values) == 0 {
    51  		return
    52  	}
    53  	min = values[0]
    54  	max = values[0]
    55  	for _, v := range values {
    56  		if max < v {
    57  			max = v
    58  		}
    59  		if min > v {
    60  			min = v
    61  		}
    62  	}
    63  	return
    64  }