mirror of
https://github.com/janet-lang/janet
synced 2025-01-12 00:20:26 +00:00
parent
32c5b816ae
commit
2a7ea27bb7
@ -668,18 +668,20 @@
|
||||
(def len (length bindings))
|
||||
(if (= 0 len) (error "expected at least 1 binding"))
|
||||
(if (odd? len) (error "expected an even number of bindings"))
|
||||
(def res (gensym))
|
||||
(defn aux [i]
|
||||
(if (>= i len)
|
||||
tru
|
||||
~(do (set ,res ,tru) true)
|
||||
(do
|
||||
(def bl (in bindings i))
|
||||
(def br (in bindings (+ 1 i)))
|
||||
(if (symbol? bl)
|
||||
~(if (def ,bl ,br) ,(aux (+ 2 i)) ,fal)
|
||||
~(if (def ,bl ,br) ,(aux (+ 2 i)))
|
||||
~(if (def ,(def sym (gensym)) ,br)
|
||||
(do (def ,bl ,sym) ,(aux (+ 2 i)))
|
||||
,fal)))))
|
||||
(aux 0))
|
||||
(do (def ,bl ,sym) ,(aux (+ 2 i))))))))
|
||||
~(do
|
||||
(var ,res nil)
|
||||
(if ,(aux 0) ,res ,fal)))
|
||||
|
||||
(defmacro when-let
|
||||
"Same as `(if-let bindings (do ;body))`."
|
||||
|
@ -130,7 +130,12 @@
|
||||
(assert (= (if-let [[a b] my-array] a) 1) "if-let 5")
|
||||
(assert (= (if-let [{:a a :b b} {:a 1 :b 2}] b) 2) "if-let 6")
|
||||
(assert (= (if-let [[a b] nil] :t :f) :f) "if-let 7")
|
||||
(assert (= (if-let [a true b false] b a) true) "if-let 8")
|
||||
|
||||
# #1191
|
||||
(var cnt 0)
|
||||
(defmacro upcnt [] (++ cnt))
|
||||
(assert (= (if-let [a true b true c true] nil (upcnt)) nil) "issue #1191")
|
||||
(assert (= cnt 1) "issue #1191")
|
||||
|
||||
(assert (= 14 (sum (map inc @[1 2 3 4]))) "sum map")
|
||||
(def myfun (juxt + - * /))
|
||||
|
Loading…
Reference in New Issue
Block a user