Modify cond to support single argument branches

This commit is contained in:
neil-lindquist 2017-06-09 20:22:23 -05:00 committed by Kodi Arfer
parent 5bf9ecfc5a
commit d3df17d9d8
4 changed files with 28 additions and 5 deletions

1
NEWS
View File

@ -32,6 +32,7 @@ Changes from 0.12.1
`(try 1 (except [ValueError] 2) (else 3))` returns `3`.
* xor: If exactly one argument is true, return it
* hy.core.reserved is now hy.extra.reserved
* cond now supports single argument branches
[ Bug Fixes ]
* All shadowed operators have the same arities as real operators

View File

@ -374,6 +374,18 @@ shows the relationship between the macro and its expansion:
(if condition-1 result-1
(if condition-2 result-2))
If only the condition is given in a branch, then the condition is also used as
the result. The expansion of this single argument version is demonstrated
below:
.. code-block:: clj
(cond [condition-1]
[condition-2])
(if condition-1 condition-1
(if condition-2 condition-2))
As shown below, only the first matching result block is executed.
.. code-block:: clj

View File

@ -56,10 +56,10 @@
(if (not (= (type branch) HyList))
(macro-error branch "cond branches need to be a list"))
(if (< (len branch) 2)
(macro-error branch "cond branches need at least two items: a test and one or more code branches"))
(setv test (first branch))
(setv thebranch (cut branch 1))
`(if ~test (do ~@thebranch)))
(do
(setv g (gensym))
`(if (do (setv ~g ~(first branch)) ~g) ~g))
`(if ~(first branch) (do ~@(cut branch 1)))))
(setv root (check-branch branch))
(setv latest-branch root)

View File

@ -291,7 +291,17 @@
(cond
[(= 1 2) (assert (is True False))]
[(is None None) (setv x True) (assert x)])
(assert (= (cond) None)))
(assert (= (cond) None))
(assert (= (cond
[False]
[[]]
[8])) 8)
;make sure test is only evaluated once
(setv x 0)
(cond [(do (+= x 1) True)])
(assert (= x 1)))
(defn test-if []