Skip to content

Commit 63d7310

Browse files
authored
Merge pull request #6 from netomi/fix-arity-of-lambda-functions
fix: correctly calculate arity of lambda functions
2 parents a8620b9 + fa0a5fd commit 63d7310

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/jsonata/functions.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1570,8 +1570,11 @@ def get_function_arity(func: Any) -> int:
15701570
from jsonata import jsonata
15711571
if isinstance(func, jsonata.Jsonata.JFunction):
15721572
return func.signature.get_min_number_of_args()
1573+
elif isinstance(func, jsonata.Jsonata.JLambda):
1574+
from inspect import signature
1575+
1576+
return len(signature(func.function).parameters)
15731577
else:
1574-
# Lambda
15751578
return len(func.arguments)
15761579

15771580
#

tests/custom_function_test.py

+9
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,12 @@ def test_ternary(self):
2222
expression = jsonata.Jsonata("$abc(a,b,c)")
2323
expression.register_lambda("abc", lambda x, y, z: str(x) + str(y) + str(z))
2424
assert expression.evaluate({"a": "a", "b": "b", "c": "c"}) == "abc"
25+
26+
def test_map_with_lambda(self):
27+
expression = jsonata.Jsonata("$map([1, 2, 3], $square)")
28+
expression.register_lambda("square", lambda x: x * x)
29+
assert expression.evaluate(None) == [1, 4, 9]
30+
31+
def test_map_with_function(self):
32+
expression = jsonata.Jsonata("$map([1, 2, 3], function($v) { $v * $v })")
33+
assert expression.evaluate(None) == [1, 4, 9]

0 commit comments

Comments
 (0)