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:
parent
ca8b6b4fe5
commit
eaf1a3023a
@ -1516,10 +1516,9 @@ xor
|
|||||||
|
|
||||||
.. versionadded:: 0.12.0
|
.. versionadded:: 0.12.0
|
||||||
|
|
||||||
``xor`` is used in logical expressions to perform exclusive or. It takes at
|
``xor`` is used in logical expressions to perform exclusive or. It takes two
|
||||||
least two parameters. It returns ``True`` if exactly one of the parameters
|
parameters. It returns ``True`` if only of the parameters is ``True``. In all
|
||||||
evaluates to ``True``. In all other cases ``False`` is returned. Example
|
other cases ``False`` is returned. Example usage:
|
||||||
usage:
|
|
||||||
|
|
||||||
.. code-block:: clj
|
.. code-block:: clj
|
||||||
|
|
||||||
@ -1529,7 +1528,7 @@ usage:
|
|||||||
=> (xor True True)
|
=> (xor True True)
|
||||||
False
|
False
|
||||||
|
|
||||||
=> (xor [] [] [0])
|
=> (xor [] [0])
|
||||||
True
|
True
|
||||||
|
|
||||||
|
|
||||||
|
@ -425,6 +425,11 @@
|
|||||||
(hyify (. value __name__))
|
(hyify (. value __name__))
|
||||||
(catch [] (string value))))))
|
(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
|
(def *exports* '[Botsbuildbots
|
||||||
butlast calling-module-name coll? cons cons? cycle
|
butlast calling-module-name coll? cons cons? cycle
|
||||||
dec distinct disassemble drop drop-last drop-while empty? even?
|
dec distinct disassemble drop drop-last drop-while empty? even?
|
||||||
@ -434,4 +439,5 @@
|
|||||||
keyword? last list* macroexpand macroexpand-1 map merge-with
|
keyword? last list* macroexpand macroexpand-1 map merge-with
|
||||||
name neg? nil? none? nth numeric? odd? pos? range read read-str
|
name neg? nil? none? nth numeric? odd? pos? range read read-str
|
||||||
remove repeat repeatedly rest reduce second some string string?
|
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])
|
||||||
|
@ -237,8 +237,3 @@
|
|||||||
(let [[decorators (slice expr nil -1)]
|
(let [[decorators (slice expr nil -1)]
|
||||||
[fndef (get expr -1)]]
|
[fndef (get expr -1)]]
|
||||||
`(with-decorator ~@decorators ~fndef)))
|
`(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))
|
|
||||||
|
@ -817,18 +817,10 @@
|
|||||||
"NATIVE: test the xor macro"
|
"NATIVE: test the xor macro"
|
||||||
(let [[xor-both-true (xor true true)]
|
(let [[xor-both-true (xor true true)]
|
||||||
[xor-both-false (xor false false)]
|
[xor-both-false (xor false false)]
|
||||||
[xor-true-false (xor true 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)]]
|
|
||||||
(assert (= xor-both-true false))
|
(assert (= xor-both-true false))
|
||||||
(assert (= xor-both-false false))
|
(assert (= xor-both-false false))
|
||||||
(assert (= xor-true-false true))
|
(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))))
|
|
||||||
|
|
||||||
(defn test-if-return-branching []
|
(defn test-if-return-branching []
|
||||||
"NATIVE: test the if return branching"
|
"NATIVE: test the if return branching"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user