Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly calculate arity of lambda functions #6

Merged
merged 3 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/jsonata/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1570,8 +1570,11 @@ def get_function_arity(func: Any) -> int:
from jsonata import jsonata
if isinstance(func, jsonata.Jsonata.JFunction):
return func.signature.get_min_number_of_args()
elif isinstance(func, jsonata.Jsonata.JLambda):
from inspect import signature

return len(signature(func.function).parameters)
else:
# Lambda
return len(func.arguments)

#
Expand Down
9 changes: 9 additions & 0 deletions tests/custom_function_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ def test_ternary(self):
expression = jsonata.Jsonata("$abc(a,b,c)")
expression.register_lambda("abc", lambda x, y, z: str(x) + str(y) + str(z))
assert expression.evaluate({"a": "a", "b": "b", "c": "c"}) == "abc"

def test_map_with_lambda(self):
expression = jsonata.Jsonata("$map([1, 2, 3], $square)")
expression.register_lambda("square", lambda x: x * x)
assert expression.evaluate(None) == [1, 4, 9]

def test_map_with_function(self):
expression = jsonata.Jsonata("$map([1, 2, 3], function($v) { $v * $v })")
assert expression.evaluate(None) == [1, 4, 9]
Loading