Make xor return single true inputs

The documentation should now be correct (#1214).
This commit is contained in:
Kodi Arfer 2017-01-31 14:31:27 -08:00 committed by Tuukka Turto
parent 7a4a2ca668
commit 9ca7f49c88
4 changed files with 36 additions and 19 deletions

10
NEWS
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"