github.com/tobgu/qframe@v0.4.0/internal/fcolumn/aggregations.go (about)

     1  package fcolumn
     2  
     3  import "math"
     4  
     5  var aggregations = map[string]func([]float64) float64{
     6  	"max": max,
     7  	"min": min,
     8  	"sum": sum,
     9  	"avg": avg,
    10  }
    11  
    12  func sum(values []float64) float64 {
    13  	result := 0.0
    14  	for _, v := range values {
    15  		result += v
    16  	}
    17  	return result
    18  }
    19  
    20  func avg(values []float64) float64 {
    21  	result := 0.0
    22  	for _, v := range values {
    23  		result += v
    24  	}
    25  
    26  	return result / float64(len(values))
    27  }
    28  
    29  func max(values []float64) float64 {
    30  	result := values[0]
    31  	for _, v := range values[1:] {
    32  		result = math.Max(result, v)
    33  	}
    34  	return result
    35  }
    36  
    37  func min(values []float64) float64 {
    38  	result := values[0]
    39  	for _, v := range values[1:] {
    40  		result = math.Min(result, v)
    41  	}
    42  	return result
    43  }