File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -143,6 +143,8 @@ Supported math helpers:
143
143
- [ Clamp] ( #clamp )
144
144
- [ Sum] ( #sum )
145
145
- [ SumBy] ( #sumby )
146
+ - [ Mean] ( #mean )
147
+ - [ MeanBy] ( #meanby )
146
148
147
149
Supported helpers for strings:
148
150
@@ -1235,6 +1237,42 @@ sum := lo.SumBy(strings, func(item string) int {
1235
1237
1236
1238
[[ play] ( https://go.dev/play/p/Dz_a_7jN_ca )]
1237
1239
1240
+ ### Mean
1241
+
1242
+ Calculates the mean of a collection of numbers.
1243
+
1244
+ If collection is empty 0 is returned.
1245
+
1246
+ ``` go
1247
+ mean := lo.Mean ([]int {2 , 3 , 4 , 5 })
1248
+ // 3
1249
+
1250
+ mean := lo.Mean ([]float64 {2 , 3 , 4 , 5 })
1251
+ // 3.5
1252
+
1253
+ mean := lo.Mean ([]float64 {})
1254
+ // 0
1255
+ ```
1256
+
1257
+ ### MeanBy
1258
+
1259
+ Calculates the mean of a collection of numbers using the given return value from the iteration function.
1260
+
1261
+ If collection is empty 0 is returned.
1262
+
1263
+ ``` go
1264
+ list := []string {" aa" , " bbb" , " cccc" , " ddddd" }
1265
+ mapper := func (item string ) float64 {
1266
+ return float64 (len (item))
1267
+ }
1268
+
1269
+ mean := lo.MeanBy (list, mapper)
1270
+ // 3.5
1271
+
1272
+ mean := lo.MeanBy ([]float64 {}, mapper)
1273
+ // 0
1274
+ ```
1275
+
1238
1276
### RandomString
1239
1277
1240
1278
Returns a random string of the specified length and made of the specified charset.
You can’t perform that action at this time.
0 commit comments