From 0e55d7d9552765975ca85cbb5e21e59130345871 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Fri, 27 Dec 2019 09:32:14 -0500 Subject: [PATCH] Allow more than two arguments to `in` or `not-in` --- NEWS.rst | 1 + hy/compiler.py | 3 +-- hy/core/shadow.hy | 14 ++++++-------- tests/native_tests/operators.hy | 3 ++- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/NEWS.rst b/NEWS.rst index be8eb70..8909d72 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -33,6 +33,7 @@ Bug Fixes ------------------------------ * Statements in the second argument of `assert` are now executed. * Fixed the expression of a while loop that contains statements being compiled twice. +* `in` and `not-in` now allow more than two arguments, as in Python. * `hy2py` can now handle format strings. * Fixed crashes from inaccessible history files. * The unit tests no longer unintentionally import the internal Python module "test". diff --git a/hy/compiler.py b/hy/compiler.py index 840f9f7..7bfcb53 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -1270,8 +1270,7 @@ class HyASTCompiler(object): return self._c_ops[k]() @special(["=", "is", "<", "<=", ">", ">="], [oneplus(FORM)]) - @special(["!=", "is-not"], [times(2, Inf, FORM)]) - @special(["in", "not-in"], [times(2, 2, FORM)]) + @special(["!=", "is-not", "in", "not-in"], [times(2, Inf, FORM)]) def compile_compare_op_expression(self, expr, root, args): if len(args) == 1: return (self.compile(args[0]) + diff --git a/hy/core/shadow.hy b/hy/core/shadow.hy index 74ab08f..85e3f56 100644 --- a/hy/core/shadow.hy +++ b/hy/core/shadow.hy @@ -117,6 +117,12 @@ (defn is-not [a1 a2 &rest a-rest] "Shadowed `is-not` keyword perform is-not on `a1` by `a2`, ..., `a-rest`." (comp-op operator.is-not a1 (+ (, a2) a-rest))) +(defn in [a1 a2 &rest a-rest] + "Shadowed `in` keyword perform `a1` in `a2` in …." + (comp-op (fn [x y] (in x y)) a1 (+ (, a2) a-rest))) +(defn not-in [a1 a2 &rest a-rest] + "Shadowed `not in` keyword perform `a1` not in `a2` not in…." + (comp-op (fn [x y] (not-in x y)) a1 (+ (, a2) a-rest))) (defn >= [a1 &rest a-rest] "Shadowed `>=` operator perform ge comparison on `a1` by each `a-rest`." (comp-op operator.ge a1 a-rest)) @@ -148,14 +154,6 @@ "Shadowed `not` keyword perform not on `x`." (not x)) -(defn in [x y] - "Shadowed `in` keyword perform `x` in `y`." - (in x y)) - -(defn not-in [x y] - "Shadowed `not in` keyword perform `x` not in `y`." - (not-in x y)) - (defn get [coll key1 &rest keys] "Access item in `coll` indexed by `key1`, with optional `keys` nested-access." (setv coll (get coll key1)) diff --git a/tests/native_tests/operators.hy b/tests/native_tests/operators.hy index e62fa57..831c292 100644 --- a/tests/native_tests/operators.hy +++ b/tests/native_tests/operators.hy @@ -294,7 +294,8 @@ (forbid (f 3)) (assert (is (f 3 [1 2]) (!= f-name "in"))) (assert (is (f 2 [1 2]) (= f-name "in"))) - (forbid (f 2 [1 2] [3 4]))) + (assert (is (f 2 [1 2] [[1 2] 3]) (= f-name "in"))) + (assert (is (f 3 [1 2] [[2 2] 3]) (!= f-name "in")))) (op-and-shadow-test [get]