summaryrefslogtreecommitdiff
path: root/examples/fourFn.py
diff options
context:
space:
mode:
authorptmcg <ptmcg@austin.rr.com>2020-04-26 10:33:12 -0500
committerptmcg <ptmcg@austin.rr.com>2020-04-26 10:33:12 -0500
commit203fa36d7ae6b79344e4bf13531b77c09f313793 (patch)
tree443459f498f38b97618344c6f707eeaa117cf670 /examples/fourFn.py
parent813ba3bed433a96e02d82cad2e2940a6850d96a5 (diff)
downloadpyparsing-git-203fa36d7ae6b79344e4bf13531b77c09f313793.tar.gz
change some lambdas to explicit methods for clarity (see discussion in #207); deleted duplicated examples (commit *all* changes this time)
Diffstat (limited to 'examples/fourFn.py')
-rw-r--r--examples/fourFn.py18
1 files changed, 16 insertions, 2 deletions
diff --git a/examples/fourFn.py b/examples/fourFn.py
index 68441b8..e448fbb 100644
--- a/examples/fourFn.py
+++ b/examples/fourFn.py
@@ -79,8 +79,13 @@ def BNF():
expr = Forward()
expr_list = delimitedList(Group(expr))
# add parse action that replaces the function identifier with a (name, number of args) tuple
+ def insert_fn_argcount_tuple(t):
+ fn = t.pop(0)
+ num_args = len(t[0])
+ t.insert(0, (fn, num_args))
+
fn_call = (ident + lpar - Group(expr_list) + rpar).setParseAction(
- lambda t: t.insert(0, (t.pop(0), len(t[0])))
+ insert_fn_argcount_tuple
)
atom = (
addop[...]
@@ -116,9 +121,14 @@ fn = {
"tan": math.tan,
"exp": math.exp,
"abs": abs,
- "trunc": lambda a: int(a),
+ "trunc": int,
"round": round,
"sgn": lambda a: -1 if a < -epsilon else 1 if a > epsilon else 0,
+ # functionsl with multiple arguments
+ "multiply": lambda a, b: a * b,
+ "hypot": math.hypot,
+ # functions with a variable number of arguments
+ "all": lambda *a: all(a),
}
@@ -211,6 +221,10 @@ if __name__ == "__main__":
test("sgn(cos(PI*3/4))", -1)
test("+(sgn(cos(PI/4)))", 1)
test("-(sgn(cos(PI/4)))", -1)
+ test("hypot(3, 4)", 5)
+ test("multiply(3, 7)", 21)
+ test("all(1,1,1)", True)
+ test("all(1,1,1,1,1,0)", False)
"""