Rewrite cond
This commit is contained in:
parent
88c0f92810
commit
6cced31738
@ -87,29 +87,19 @@ Shorthand for nested with/a* loops:
|
|||||||
|
|
||||||
The result in the bracket may be omitted, in which case the condition is also
|
The result in the bracket may be omitted, in which case the condition is also
|
||||||
used as the result."
|
used as the result."
|
||||||
(if (empty? branches)
|
(or branches
|
||||||
None
|
(return))
|
||||||
(do
|
|
||||||
(setv branches (iter branches))
|
|
||||||
(setv branch (next branches))
|
|
||||||
(defn check-branch [branch]
|
|
||||||
"check `cond` branch for validity, return the corresponding `if` expr"
|
|
||||||
(if (not (= (type branch) HyList))
|
|
||||||
(macro-error branch "cond branches need to be a list"))
|
|
||||||
(if (< (len branch) 2)
|
|
||||||
(do
|
|
||||||
(setv g (gensym))
|
|
||||||
`(if (do (setv ~g ~(first branch)) ~g) ~g))
|
|
||||||
`(if ~(first branch) (do ~@(cut branch 1)))))
|
|
||||||
|
|
||||||
(setv root (check-branch branch))
|
`(if ~@(reduce + (gfor
|
||||||
(setv latest-branch root)
|
branch branches
|
||||||
|
(if
|
||||||
(for [branch branches]
|
(not (and (is (type branch) hy.HyList) branch))
|
||||||
(setv cur-branch (check-branch branch))
|
(macro-error branch "each cond branch needs to be a nonempty list")
|
||||||
(.append latest-branch cur-branch)
|
(= (len branch) 1) (do
|
||||||
(setv latest-branch cur-branch))
|
(setv g (gensym))
|
||||||
root)))
|
[`(do (setv ~g ~(first branch)) ~g) g])
|
||||||
|
True
|
||||||
|
[(first branch) `(do ~@(cut branch 1))])))))
|
||||||
|
|
||||||
|
|
||||||
(defmacro -> [head &rest args]
|
(defmacro -> [head &rest args]
|
||||||
|
Loading…
Reference in New Issue
Block a user