@@ -628,14 +628,24 @@ Groups the elements of an array by the result of the [predicate](#predicate).
628
628
groupBy(users, .Age)
629
629
```
630
630
631
- ### count(array, predicate) {#count}
631
+ ### count(array[ , predicate] ) {#count}
632
632
633
633
Returns the number of elements what satisfies the [ predicate] ( #predicate ) .
634
634
635
+ ``` expr
636
+ count(users, .Age > 18)
637
+ ```
638
+
635
639
Equivalent to:
636
640
637
641
``` expr
638
- len(filter(array, predicate))
642
+ len(filter(users, .Age > 18))
643
+ ```
644
+
645
+ If the predicate is not given, returns the number of ` true ` elements in the array.
646
+
647
+ ``` expr
648
+ count([true, false, true]) == 2
639
649
```
640
650
641
651
### concat(array1, array2[ , ...] ) {#concat}
@@ -673,14 +683,29 @@ reduce(1..9, #acc + #)
673
683
reduce(1..9, #acc + #, 0)
674
684
```
675
685
676
- ### sum(array) {#sum}
686
+ ### sum(array[ , predicate ] ) {#sum}
677
687
678
688
Returns the sum of all numbers in the array.
679
689
680
690
``` expr
681
691
sum([1, 2, 3]) == 6
682
692
```
683
693
694
+ If the optional ` predicate ` argument is given, it is a predicate that is applied on each element
695
+ of the array before summing.
696
+
697
+ ``` expr
698
+ sum(accounts, .Balance)
699
+ ```
700
+
701
+ Equivalent to:
702
+
703
+ ``` expr
704
+ reduce(accounts, #acc + .Balance, 0)
705
+ // or
706
+ sum(map(accounts, .Balance))
707
+ ```
708
+
684
709
### mean(array) {#mean}
685
710
686
711
Returns the average of all numbers in the array.
0 commit comments