Change xor to binary function

xor with more than two input parameters is not well defined and people
have different expectations on how it should behave. Avoid confusion by
sticking with two parameters only.
This commit is contained in:
Tuukka Turto 2015-08-03 05:37:39 +03:00
parent ca8b6b4fe5
commit eaf1a3023a
4 changed files with 13 additions and 21 deletions

View File

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

View File

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

View File

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

View File

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