Skip to content

Commit 888d71d

Browse files
enhance $reduce to support 4 arg function
1 parent 32b3836 commit 888d71d

File tree

6 files changed

+44
-3
lines changed

6 files changed

+44
-3
lines changed

src/functions.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,8 @@ const functions = (() => {
15941594

15951595
var result;
15961596

1597-
if (getFunctionArity(func) !== 2) {
1597+
var arity = getFunctionArity(func);
1598+
if (arity < 2) {
15981599
throw {
15991600
stack: (new Error()).stack,
16001601
code: "D3050",
@@ -1612,7 +1613,14 @@ const functions = (() => {
16121613
}
16131614

16141615
while (index < sequence.length) {
1615-
result = yield* func.apply(this, [result, sequence[index]]);
1616+
var args = [result, sequence[index]];
1617+
if (arity >= 3) {
1618+
args.push(index);
1619+
}
1620+
if (arity >= 4) {
1621+
args.push(sequence);
1622+
}
1623+
result = yield* func.apply(this, args);
16161624
index++;
16171625
}
16181626

src/jsonata.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1787,7 +1787,7 @@ var jsonata = (function() {
17871787
"D3020": "Third argument of split function must evaluate to a positive number",
17881788
"D3030": "Unable to cast value to a number: {{value}}",
17891789
"D3040": "Third argument of match function must evaluate to a positive number",
1790-
"D3050": "First argument of reduce function must be a function with two arguments",
1790+
"D3050": "The second argument of reduce function must be a function with at least two arguments",
17911791
"D3060": "The sqrt function cannot be applied to a negative number: {{value}}",
17921792
"D3061": "The power function has resulted in a value that cannot be represented as a JSON number: base={{value}}, exponent={{exp}}",
17931793
"D3070": "The single argument form of the sort function can only be applied to an array of strings or an array of numbers. Use the second argument to specify a comparison function",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr-file": "case009.jsonata",
3+
"data": null,
4+
"bindings": {},
5+
"result": 4
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(
2+
$months := [
3+
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4+
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
5+
];
6+
$indexof := λ($array, $value) {
7+
$reduce($array, λ($acc, $v, $i) {
8+
$v=$value ? $i : $acc
9+
})
10+
};
11+
$indexof($months, 'May')
12+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr-file": "case010.jsonata",
3+
"data": null,
4+
"bindings": {},
5+
"result": 6
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(
2+
$mean := $reduce(?, λ($acc, $v, $i, $arr) {(
3+
$total := $acc + $v;
4+
$length := $count($arr);
5+
$i = $length - 1 ? $total / $length : $total
6+
)});
7+
8+
$mean([7,3,8])
9+
)

0 commit comments

Comments
 (0)