Merge pull request #872 from tuturto/xor

Add exclusive or logical operator
This commit is contained in:
Paul‮etnomailgaT‭ 2015-12-12 12:38:14 -05:00
commit 3eb6001852
3 changed files with 36 additions and 1 deletions

View File

@ -1489,6 +1489,27 @@ expands to:
Section :ref:`using-gensym` Section :ref:`using-gensym`
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:
.. code-block:: clj
=> (xor True False)
True
=> (xor True True)
False
=> (xor [] [0])
True
yield yield
----- -----

View File

@ -463,6 +463,11 @@
(hyify (. value __name__)) (hyify (. value __name__))
(except [] (string value)))))) (except [] (string value))))))
(defn xor [a b]
"Perform exclusive or between two parameters"
(or (and a (not b))
(and b (not a))))
(def *exports* (def *exports*
'[*map accumulate butlast calling-module-name chain coll? combinations '[*map accumulate butlast calling-module-name chain coll? combinations
compress cons cons? count cycle dec distinct disassemble drop drop-last compress cons cons? count cycle dec distinct disassemble drop drop-last
@ -472,4 +477,4 @@
last list* macroexpand macroexpand-1 map merge-with multicombinations name last list* macroexpand macroexpand-1 map merge-with multicombinations name
neg? nil? none? nth numeric? odd? partition permutations pos? product range neg? nil? none? nth numeric? odd? partition permutations pos? product range
read read-str remove repeat repeatedly rest reduce second some string read read-str remove repeat repeatedly rest reduce second some string
string? symbol? take take-nth take-while tee zero? zip zip-longest]) string? symbol? take take-nth take-while xor tee zero? zip zip-longest])

View File

@ -927,6 +927,15 @@
(assert (= a 1))) (assert (= a 1)))
(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))))
(defn test-if-return-branching [] (defn test-if-return-branching []
"NATIVE: test the if return branching" "NATIVE: test the if return branching"
; thanks, algernon ; thanks, algernon