diff --git a/NEWS b/NEWS index 69c61db..15775f0 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,13 @@ +Changes from 0.12.1 + + [ Language Changes ] + * xor: If exactly one argument is true, return it + +Changes from 0.12.0 + + [ Bug Fixes ] + * Allow installation without Git + Changes from 0.11.0 This release brings some quite significant changes on the language and as a diff --git a/docs/language/api.rst b/docs/language/api.rst index 7df26fe..ee91be3 100644 --- a/docs/language/api.rst +++ b/docs/language/api.rst @@ -1713,20 +1713,15 @@ xor .. versionadded:: 0.12.0 -``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: +``xor`` performs the logical operation of exclusive OR. It takes two arguments. +If exactly one argument is true, that argument is returned. If neither is true, +the second argument is returned (which will necessarily be false). Otherwise, +when both arguments are true, the value ``False`` is returned. .. code-block:: clj - => (xor True False) - True - - => (xor True True) - False - - => (xor [] [0]) - True + => [(xor 0 0) (xor 0 1) (xor 1 0) (xor 1 1)] + [0, 1, 1, False] yield diff --git a/hy/core/language.hy b/hy/core/language.hy index 5cb546c..5628e3f 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -481,8 +481,9 @@ (defn xor [a b] "Perform exclusive or between two parameters" - (or (and a (not b)) - (and b (not a)))) + (if (and a b) + False + (or a b))) (def *exports* '[*map accumulate butlast calling-module-name chain coll? combinations diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 6893b4b..3d5124c 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -1019,12 +1019,23 @@ (defn test-xor [] "NATIVE: test the xor macro" - (let [xor-both-true (xor True True) - xor-both-false (xor False False) - xor-true-false (xor True False)] - (assert (= xor-both-true False)) - (assert (= xor-both-false False)) - (assert (= xor-true-false True)))) + + ; Test each cell of the truth table. + (assert (is (xor False False) False)) + (assert (is (xor False True) True)) + (assert (is (xor True False) True)) + (assert (is (xor True True) False)) + + ; Same thing, but with numbers. + (assert (is (xor 0 0) 0)) + (assert (is (xor 0 1) 1)) + (assert (is (xor 1 0) 1)) + (assert (is (xor 1 1) False)) + + ; Of two distinct false values, the second is returned. + (assert (is (xor False 0) 0)) + (assert (is (xor 0 False) False))) + (defn test-if-return-branching [] "NATIVE: test the if return branching"