Skip to content

Commit 96c7f9c

Browse files
authored
Update README.md
1 parent b23221e commit 96c7f9c

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ Supported math helpers:
143143
- [Clamp](#clamp)
144144
- [Sum](#sum)
145145
- [SumBy](#sumby)
146+
- [Mean](#mean)
147+
- [MeanBy](#meanby)
146148

147149
Supported helpers for strings:
148150

@@ -1235,6 +1237,42 @@ sum := lo.SumBy(strings, func(item string) int {
12351237

12361238
[[play](https://go.dev/play/p/Dz_a_7jN_ca)]
12371239

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+
12381276
### RandomString
12391277

12401278
Returns a random string of the specified length and made of the specified charset.

0 commit comments

Comments
 (0)