mirror of
https://github.com/janet-lang/janet
synced 2025-01-12 16:40:27 +00:00
Merge pull request #1188 from primo-ppcg/if-let
This commit is contained in:
commit
7272f43191
@ -674,21 +674,7 @@
|
|||||||
(do
|
(do
|
||||||
(def bl (in bindings i))
|
(def bl (in bindings i))
|
||||||
(def br (in bindings (+ 1 i)))
|
(def br (in bindings (+ 1 i)))
|
||||||
(def atm (idempotent? bl))
|
(tuple 'if (tuple 'def bl br) (aux (+ 2 i)) fal))))
|
||||||
(def sym (if atm bl (gensym)))
|
|
||||||
(if atm
|
|
||||||
# Simple binding
|
|
||||||
(tuple 'do
|
|
||||||
(tuple 'def sym br)
|
|
||||||
(tuple 'if sym (aux (+ 2 i)) fal))
|
|
||||||
# Destructured binding
|
|
||||||
(tuple 'do
|
|
||||||
(tuple 'def sym br)
|
|
||||||
(tuple 'if sym
|
|
||||||
(tuple 'do
|
|
||||||
(tuple 'def bl sym)
|
|
||||||
(aux (+ 2 i)))
|
|
||||||
fal))))))
|
|
||||||
(aux 0))
|
(aux 0))
|
||||||
|
|
||||||
(defmacro when-let
|
(defmacro when-let
|
||||||
|
@ -123,10 +123,12 @@
|
|||||||
# Some higher order functions and macros
|
# Some higher order functions and macros
|
||||||
# 5e2de33
|
# 5e2de33
|
||||||
(def my-array @[1 2 3 4 5 6])
|
(def my-array @[1 2 3 4 5 6])
|
||||||
(def x (if-let [x (get my-array 5)] x))
|
(assert (= (if-let [x (get my-array 5)] x) 6) "if-let 1")
|
||||||
(assert (= x 6) "if-let")
|
(assert (= (if-let [y (get @{} :key)] 10 nil) nil) "if-let 2")
|
||||||
(def x (if-let [y (get @{} :key)] 10 nil))
|
(assert (= (if-let [a my-array k (next a)] :t :f) :t) "if-let 3")
|
||||||
(assert (not x) "if-let 2")
|
(assert (= (if-let [a my-array k (next a 5)] :t :f) :f) "if-let 4")
|
||||||
|
(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 (= 14 (sum (map inc @[1 2 3 4]))) "sum map")
|
(assert (= 14 (sum (map inc @[1 2 3 4]))) "sum map")
|
||||||
(def myfun (juxt + - * /))
|
(def myfun (juxt + - * /))
|
||||||
|
Loading…
Reference in New Issue
Block a user