remove lisp-if / lisp-if-not in favor of lif / lif-not
This commit is contained in:
parent
4cdfdfbafe
commit
7d8ddd9ecb
@ -881,47 +881,39 @@ an empty sequence, and an empty dictionary are considered ``False``; everything
|
|||||||
else is considered ``True``.
|
else is considered ``True``.
|
||||||
|
|
||||||
|
|
||||||
lisp-if / lif and lisp-if-not / lif-not
|
lif and lif-not
|
||||||
---------------------------------------
|
---------------------------------------
|
||||||
|
|
||||||
.. versionadded:: 0.10.0
|
.. versionadded:: 0.10.0
|
||||||
|
|
||||||
.. versionadded:: 0.11.0
|
.. versionadded:: 0.11.0
|
||||||
lisp-if-not / lif-not
|
lif-not
|
||||||
|
|
||||||
For those that prefer a more Lispy ``if`` clause, we have ``lisp-if``, or
|
For those that prefer a more Lispy ``if`` clause, we have, or
|
||||||
``lif``. This *only* considers ``None`` / ``nil`` to be false! All other
|
``lif``. This *only* considers ``None`` / ``nil`` to be false! All other
|
||||||
"false-ish" Python values are considered true. Conversely, we have
|
"false-ish" Python values are considered true. Conversely, we have
|
||||||
``lisp-if-not`` and ``lif-not`` in parallel to ``if`` and ``if-not`` which
|
``lif-not`` in parallel to ``if`` and ``if-not`` which
|
||||||
reverses the comparison.
|
reverses the comparison.
|
||||||
|
|
||||||
|
|
||||||
.. code-block:: clj
|
.. code-block:: clj
|
||||||
|
|
||||||
=> (lisp-if True "true" "false")
|
|
||||||
"true"
|
|
||||||
=> (lisp-if False "true" "false")
|
|
||||||
"true"
|
|
||||||
=> (lisp-if 0 "true" "false")
|
|
||||||
"true"
|
|
||||||
=> (lisp-if nil "true" "false")
|
|
||||||
"false"
|
|
||||||
=> (lisp-if None "true" "false")
|
|
||||||
"false"
|
|
||||||
=> (lisp-if-not nil "true" "false")
|
|
||||||
"true"
|
|
||||||
=> (lisp-if-not None "true" "false")
|
|
||||||
"true"
|
|
||||||
=> (lisp-if-not False "true" "false")
|
|
||||||
"false"
|
|
||||||
|
|
||||||
; Equivalent but shorter
|
|
||||||
=> (lif True "true" "false")
|
=> (lif True "true" "false")
|
||||||
"true"
|
"true"
|
||||||
|
=> (lif False "true" "false")
|
||||||
|
"true"
|
||||||
|
=> (lif 0 "true" "false")
|
||||||
|
"true"
|
||||||
=> (lif nil "true" "false")
|
=> (lif nil "true" "false")
|
||||||
"false"
|
"false"
|
||||||
|
=> (lif None "true" "false")
|
||||||
|
"false"
|
||||||
|
=> (lif-not nil "true" "false")
|
||||||
|
"true"
|
||||||
=> (lif-not None "true" "false")
|
=> (lif-not None "true" "false")
|
||||||
"true"
|
"true"
|
||||||
|
=> (lif-not False "true" "false")
|
||||||
|
"false"
|
||||||
|
|
||||||
|
|
||||||
import
|
import
|
||||||
|
@ -149,11 +149,11 @@
|
|||||||
`(if (not ~test) ~not-branch ~yes-branch)))
|
`(if (not ~test) ~not-branch ~yes-branch)))
|
||||||
|
|
||||||
|
|
||||||
(defmacro-alias [lisp-if lif] [test &rest branches]
|
(defmacro lif [test &rest branches]
|
||||||
"Like `if`, but anything that is not None/nil is considered true."
|
"Like `if`, but anything that is not None/nil is considered true."
|
||||||
`(if (is-not ~test nil) ~@branches))
|
`(if (is-not ~test nil) ~@branches))
|
||||||
|
|
||||||
(defmacro-alias [lisp-if-not lif-not] [test &rest branches]
|
(defmacro lif-not [test &rest branches]
|
||||||
"Like `if-not`, but anything that is not None/nil is considered true."
|
"Like `if-not`, but anything that is not None/nil is considered true."
|
||||||
`(if (is ~test nil) ~@branches))
|
`(if (is ~test nil) ~@branches))
|
||||||
|
|
||||||
|
@ -188,39 +188,35 @@
|
|||||||
:yes)))
|
:yes)))
|
||||||
|
|
||||||
|
|
||||||
(defn test-lisp-if []
|
(defn test-lif []
|
||||||
"test that lisp-if works as expected"
|
"test that lif works as expected"
|
||||||
; nil is false
|
; nil is false
|
||||||
(assert (= (lisp-if None "true" "false") "false"))
|
(assert (= (lif None "true" "false") "false"))
|
||||||
(assert (= (lisp-if nil "true" "false") "false"))
|
(assert (= (lif nil "true" "false") "false"))
|
||||||
|
|
||||||
; But everything else is True! Even falsey things.
|
; But everything else is True! Even falsey things.
|
||||||
(assert (= (lisp-if True "true" "false") "true"))
|
(assert (= (lif True "true" "false") "true"))
|
||||||
(assert (= (lisp-if False "true" "false") "true"))
|
(assert (= (lif False "true" "false") "true"))
|
||||||
(assert (= (lisp-if 0 "true" "false") "true"))
|
(assert (= (lif 0 "true" "false") "true"))
|
||||||
(assert (= (lisp-if "some-string" "true" "false") "true"))
|
(assert (= (lif "some-string" "true" "false") "true"))
|
||||||
(assert (= (lisp-if "" "true" "false") "true"))
|
(assert (= (lif "" "true" "false") "true"))
|
||||||
(assert (= (lisp-if (+ 1 2 3) "true" "false") "true"))
|
(assert (= (lif (+ 1 2 3) "true" "false") "true"))
|
||||||
|
|
||||||
; Just to be sure, test the alias lif
|
|
||||||
(assert (= (lif nil "true" "false") "false"))
|
(assert (= (lif nil "true" "false") "false"))
|
||||||
(assert (= (lif 0 "true" "false") "true")))
|
(assert (= (lif 0 "true" "false") "true")))
|
||||||
|
|
||||||
(defn test-lisp-if-not []
|
(defn test-lif-not []
|
||||||
"test that lisp-if-not works as expected"
|
"test that lif-not works as expected"
|
||||||
; nil is false
|
; nil is false
|
||||||
(assert (= (lisp-if-not None "false" "true") "false"))
|
(assert (= (lif-not None "false" "true") "false"))
|
||||||
(assert (= (lisp-if-not nil "false" "true") "false"))
|
(assert (= (lif-not nil "false" "true") "false"))
|
||||||
|
|
||||||
; But everything else is True! Even falsey things.
|
; But everything else is True! Even falsey things.
|
||||||
(assert (= (lisp-if-not True "false" "true") "true"))
|
(assert (= (lif-not True "false" "true") "true"))
|
||||||
(assert (= (lisp-if-not False "false" "true") "true"))
|
(assert (= (lif-not False "false" "true") "true"))
|
||||||
(assert (= (lisp-if-not 0 "false" "true") "true"))
|
(assert (= (lif-not 0 "false" "true") "true"))
|
||||||
(assert (= (lisp-if-not "some-string" "false" "true") "true"))
|
(assert (= (lif-not "some-string" "false" "true") "true"))
|
||||||
(assert (= (lisp-if-not "" "false" "true") "true"))
|
(assert (= (lif-not "" "false" "true") "true"))
|
||||||
(assert (= (lisp-if-not (+ 1 2 3) "false" "true") "true"))
|
(assert (= (lif-not (+ 1 2 3) "false" "true") "true"))
|
||||||
|
|
||||||
; Just to be sure, test the alias lif-not
|
|
||||||
(assert (= (lif-not nil "false" "true") "false"))
|
(assert (= (lif-not nil "false" "true") "false"))
|
||||||
(assert (= (lif-not 0 "false" "true") "true")))
|
(assert (= (lif-not 0 "false" "true") "true")))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user