diff --git a/docs/language/api.rst b/docs/language/api.rst index ab58065..5ddbe39 100644 --- a/docs/language/api.rst +++ b/docs/language/api.rst @@ -1516,10 +1516,9 @@ xor .. versionadded:: 0.12.0 -``xor`` is used in logical expressions to perform exclusive or. It takes at -least two parameters. It returns ``True`` if exactly one of the parameters -evaluates to ``True``. In all other cases ``False`` is returned. Example -usage: +``xor`` is used in logical expressions to perform exclusive or. It takes two +parameters. It returns ``True`` if only of the parameters is ``True``. In all +other cases ``False`` is returned. Example usage: .. code-block:: clj @@ -1529,7 +1528,7 @@ usage: => (xor True True) False - => (xor [] [] [0]) + => (xor [] [0]) True diff --git a/hy/core/language.hy b/hy/core/language.hy index d70e84b..ad5ee35 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -425,6 +425,11 @@ (hyify (. value __name__)) (catch [] (string value)))))) +(defn xor [a b] + "Perform exclusive or between two parameters" + (or (and a (not b)) + (and b (not a)))) + (def *exports* '[Botsbuildbots butlast calling-module-name coll? cons cons? cycle dec distinct disassemble drop drop-last drop-while empty? even? @@ -434,4 +439,5 @@ keyword? last list* macroexpand macroexpand-1 map merge-with name neg? nil? none? nth numeric? odd? pos? range read read-str remove repeat repeatedly rest reduce second some string string? - symbol? take take-nth take-while zero? zip zip_longest zipwith]) + symbol? take take-nth take-while xor zero? zip zip_longest + zipwith]) diff --git a/hy/core/macros.hy b/hy/core/macros.hy index dce89c6..014920c 100644 --- a/hy/core/macros.hy +++ b/hy/core/macros.hy @@ -237,8 +237,3 @@ (let [[decorators (slice expr nil -1)] [fndef (get expr -1)]] `(with-decorator ~@decorators ~fndef))) - -(defmacro xor [&rest args] - "perform exclusive or comparison between all arguments" - (when (< (len args) 2) (macro-error nil "xor requires at least two arguments.")) - `(= (reduce (fn [a b] (if b (inc a) a)) ~args 0) 1)) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 5ced0cf..00b7a99 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -817,18 +817,10 @@ "NATIVE: test the xor macro" (let [[xor-both-true (xor true true)] [xor-both-false (xor false false)] - [xor-true-false (xor true false)] - [xor-one-true (xor false true false)] - [xor-one-false (xor true false true)] - [xor-all-true (xor true true true)] - [xor-all-false (xor false false false)]] + [xor-true-false (xor true false)]] (assert (= xor-both-true false)) (assert (= xor-both-false false)) - (assert (= xor-true-false true)) - (assert (= xor-one-true true)) - (assert (= xor-one-false false)) - (assert (= xor-all-true false)) - (assert (= xor-all-false false)))) + (assert (= xor-true-false true)))) (defn test-if-return-branching [] "NATIVE: test the if return branching"