cuelang.org/go@v0.10.1/pkg/list/math.go (about) 1 // Copyright 2018 The CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package list 16 17 import ( 18 "fmt" 19 20 "github.com/cockroachdb/apd/v3" 21 22 "cuelang.org/go/internal" 23 ) 24 25 // Avg returns the average value of a non empty list xs. 26 func Avg(xs []*internal.Decimal) (*internal.Decimal, error) { 27 if len(xs) == 0 { 28 return nil, fmt.Errorf("empty list") 29 } 30 31 s := apd.New(0, 0) 32 for _, x := range xs { 33 _, err := internal.BaseContext.Add(s, x, s) 34 if err != nil { 35 return nil, err 36 } 37 } 38 39 var d apd.Decimal 40 l := apd.New(int64(len(xs)), 0) 41 _, err := internal.BaseContext.Quo(&d, s, l) 42 if err != nil { 43 return nil, err 44 } 45 return &d, nil 46 } 47 48 // Max returns the maximum value of a non empty list xs. 49 func Max(xs []*internal.Decimal) (*internal.Decimal, error) { 50 if len(xs) == 0 { 51 return nil, fmt.Errorf("empty list") 52 } 53 54 max := xs[0] 55 for _, x := range xs[1:] { 56 if max.Cmp(x) == -1 { 57 max = x 58 } 59 } 60 return max, nil 61 } 62 63 // Min returns the minimum value of a non empty list xs. 64 func Min(xs []*internal.Decimal) (*internal.Decimal, error) { 65 if len(xs) == 0 { 66 return nil, fmt.Errorf("empty list") 67 } 68 69 min := xs[0] 70 for _, x := range xs[1:] { 71 if min.Cmp(x) == +1 { 72 min = x 73 } 74 } 75 return min, nil 76 } 77 78 // Product returns the product of a non empty list xs. 79 func Product(xs []*internal.Decimal) (*internal.Decimal, error) { 80 d := apd.New(1, 0) 81 for _, x := range xs { 82 _, err := internal.BaseContext.Mul(d, x, d) 83 if err != nil { 84 return nil, err 85 } 86 } 87 return d, nil 88 } 89 90 // Range generates a list of numbers using a start value, a limit value, and a 91 // step value. 92 // 93 // For instance: 94 // 95 // Range(0, 5, 2) 96 // 97 // results in 98 // 99 // [0, 2, 4] 100 func Range(start, limit, step *internal.Decimal) ([]*internal.Decimal, error) { 101 if step.IsZero() { 102 return nil, fmt.Errorf("step must be non zero") 103 } 104 105 if !step.Negative && start.Cmp(limit) == +1 { 106 return nil, fmt.Errorf("end must be greater than start when step is positive") 107 } 108 109 if step.Negative && start.Cmp(limit) == -1 { 110 return nil, fmt.Errorf("end must be less than start when step is negative") 111 } 112 113 var vals []*internal.Decimal 114 num := start 115 for { 116 if !step.Negative && num.Cmp(limit) != -1 { 117 break 118 } 119 120 if step.Negative && num.Cmp(limit) != +1 { 121 break 122 } 123 124 vals = append(vals, num) 125 d := apd.New(0, 0) 126 _, err := internal.BaseContext.Add(d, step, num) 127 if err != nil { 128 return nil, err 129 } 130 num = d 131 } 132 return vals, nil 133 } 134 135 // Sum returns the sum of a list non empty xs. 136 func Sum(xs []*internal.Decimal) (*internal.Decimal, error) { 137 d := apd.New(0, 0) 138 for _, x := range xs { 139 _, err := internal.BaseContext.Add(d, x, d) 140 if err != nil { 141 return nil, err 142 } 143 } 144 return d, nil 145 }