github.com/blend/go-sdk@v1.20220411.3/mathutil/sum.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 (
    11  	"time"
    12  )
    13  
    14  // Sum adds all the numbers of a slice together
    15  func Sum(input []float64) float64 {
    16  	var total float64
    17  	if len(input) == 0 {
    18  		return 0
    19  	}
    20  	// Add em up
    21  	for _, n := range input {
    22  		total += n
    23  	}
    24  
    25  	return total
    26  }
    27  
    28  // SumInts adds all the numbers of a slice together
    29  func SumInts(values []int) int {
    30  	var total int
    31  	for x := 0; x < len(values); x++ {
    32  		total += values[x]
    33  	}
    34  
    35  	return total
    36  }
    37  
    38  // SumDurations adds all the numbers of a slice together
    39  func SumDurations(values []time.Duration) time.Duration {
    40  	var total time.Duration
    41  	for x := 0; x < len(values); x++ {
    42  		total += values[x]
    43  	}
    44  
    45  	return total
    46  }