Skip to content

Commit 725b702

Browse files
committed
Make count() predicate optional
1 parent d236dcd commit 725b702

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

checker/checker.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,10 @@ func (v *checker) BuiltinNode(node *ast.BuiltinNode) (reflect.Type, info) {
653653
return v.error(node.Arguments[0], "builtin %v takes only array (got %v)", node.Name, collection)
654654
}
655655

656+
if len(node.Arguments) == 1 {
657+
return integerType, info{}
658+
}
659+
656660
v.begin(collection)
657661
closure, _ := v.visit(node.Arguments[1])
658662
v.end()

compiler/compiler.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,11 @@ func (c *compiler) BuiltinNode(node *ast.BuiltinNode) {
800800
c.compile(node.Arguments[0])
801801
c.emit(OpBegin)
802802
c.emitLoop(func() {
803-
c.compile(node.Arguments[1])
803+
if len(node.Arguments) == 2 {
804+
c.compile(node.Arguments[1])
805+
} else {
806+
c.emit(OpPointer)
807+
}
804808
c.emitCond(func() {
805809
c.emit(OpIncrementCount)
806810
})

docs/language-definition.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,14 +628,24 @@ Groups the elements of an array by the result of the [predicate](#predicate).
628628
groupBy(users, .Age)
629629
```
630630

631-
### count(array, predicate) {#count}
631+
### count(array[, predicate]) {#count}
632632

633633
Returns the number of elements what satisfies the [predicate](#predicate).
634634

635+
```expr
636+
count(users, .Age > 18)
637+
```
638+
635639
Equivalent to:
636640

637641
```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
639649
```
640650

641651
### concat(array1, array2[, ...]) {#concat}
@@ -673,14 +683,29 @@ reduce(1..9, #acc + #)
673683
reduce(1..9, #acc + #, 0)
674684
```
675685

676-
### sum(array) {#sum}
686+
### sum(array[, predicate]) {#sum}
677687

678688
Returns the sum of all numbers in the array.
679689

680690
```expr
681691
sum([1, 2, 3]) == 6
682692
```
683693

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+
684709
### mean(array) {#mean}
685710

686711
Returns the average of all numbers in the array.

expr_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,10 @@ func TestExpr(t *testing.T) {
905905
`count(1..30, {# % 3 == 0})`,
906906
10,
907907
},
908+
{
909+
`count([true, true, false])`,
910+
2,
911+
},
908912
{
909913
`"a" < "b"`,
910914
true,

parser/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var predicates = map[string]struct {
3333
"one": {[]arg{expr, closure}},
3434
"filter": {[]arg{expr, closure}},
3535
"map": {[]arg{expr, closure}},
36-
"count": {[]arg{expr, closure}},
36+
"count": {[]arg{expr, closure | optional}},
3737
"sum": {[]arg{expr, closure | optional}},
3838
"find": {[]arg{expr, closure}},
3939
"findIndex": {[]arg{expr, closure}},

0 commit comments

Comments
 (0)