Merge branch 'master' into pr/515

This commit is contained in:
Paul Tagliamonte 2014-04-10 22:52:41 -04:00
commit 217f3ad65f
3 changed files with 52 additions and 0 deletions

View File

@ -726,6 +726,34 @@ any numeric type, empty sequence and empty dictionary are considered `False`.
Everything else is considered `True`.
lisp-if / lif
-------------
For those that prefer a more lisp-y if clause, we have lisp-if, or lif. This
*only* considers None/nil as false! All other values of python
"falseiness" are considered true.
.. 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"
; And, same thing
=> (lif True "true" "false")
"true"
=> (lif nil "true" "false")
"false"
import
------

View File

@ -145,6 +145,11 @@
`(if (not ~test) ~not-branch ~yes-branch)))
(defmacro-alias [lisp-if lif] [test &rest branches]
"Like `if`, but anything that is not None/nil is considered true."
`(if (is-not ~test nil) ~@branches))
(defmacro when [test &rest body]
"Execute `body` when `test` is true"
`(if ~test (do ~@body)))

View File

@ -201,6 +201,25 @@
:yes)))
(defn test-lisp-if []
"test that lisp-if works as expected"
; nil is false
(assert (= (lisp-if None "true" "false") "false"))
(assert (= (lisp-if nil "true" "false") "false"))
; But everything else is True! Even falsey things.
(assert (= (lisp-if True "true" "false") "true"))
(assert (= (lisp-if False "true" "false") "true"))
(assert (= (lisp-if 0 "true" "false") "true"))
(assert (= (lisp-if "some-string" "true" "false") "true"))
(assert (= (lisp-if "" "true" "false") "true"))
(assert (= (lisp-if (+ 1 2 3) "true" "false") "true"))
; Just to be sure, test the alias lif
(assert (= (lif nil "true" "false") "false"))
(assert (= (lif 0 "true" "false") "true")))
(defn test-defn-alias []
(defn-alias [tda-main tda-a1 tda-a2] [] :bazinga)
(defun-alias [tda-main tda-a1 tda-a2] [] :bazinga)