mirror of
https://github.com/janet-lang/janet
synced 2025-02-03 02:39:09 +00:00
Fix if-let when-let and add put-in (assoc-in)
This commit is contained in:
parent
1bc5705667
commit
5ff0367d0f
22
examples/utils.dst
Normal file
22
examples/utils.dst
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
(defn put-in [coll keys val]
|
||||||
|
(defn assoc [the-coll n]
|
||||||
|
(if-let [current-key (get keys n)
|
||||||
|
current-val (get the-coll current-key)]
|
||||||
|
(put the-coll current-key (assoc current-val (inc n)))
|
||||||
|
val))
|
||||||
|
(assoc coll 0))
|
||||||
|
|
||||||
|
|
||||||
|
(defn update-in [coll keys an-fn]
|
||||||
|
(def new-keys (array-slice coll 0 -2) )
|
||||||
|
(def last-key (get (array-slice coll -1 -2) 0))
|
||||||
|
(defn assoc [the-coll n]
|
||||||
|
(if-let [current-key (get keys n)
|
||||||
|
current-val (get the-coll current-key)]
|
||||||
|
(put the-coll current-key (assoc current-val (inc n)))
|
||||||
|
( update the-coll last-key an-fn )))
|
||||||
|
(assoc coll new-keys 0))
|
||||||
|
|
||||||
|
|
||||||
|
;; (defn update-in-test [ ] (update-in @{:a "x" :b {:y {"pipa" 3}}} [:b :y "pipa"] type))
|
@ -371,7 +371,6 @@ If no match is found, returns nil"
|
|||||||
all the forms with let and evaluates the first expression else
|
all the forms with let and evaluates the first expression else
|
||||||
evaluates the second"
|
evaluates the second"
|
||||||
[bindings then else]
|
[bindings then else]
|
||||||
#(tuple 'print (tuple '> (tuple 'length bindings) 2))
|
|
||||||
(tuple 'let bindings
|
(tuple 'let bindings
|
||||||
(tuple 'if (tuple 'and (tuple 'get bindings 1)
|
(tuple 'if (tuple 'and (tuple 'get bindings 1)
|
||||||
(tuple 'if
|
(tuple 'if
|
||||||
@ -384,8 +383,7 @@ If no match is found, returns nil"
|
|||||||
"Takes the first one or two forms in vector and if true binds
|
"Takes the first one or two forms in vector and if true binds
|
||||||
all the forms with let and evaluates the body"
|
all the forms with let and evaluates the body"
|
||||||
[bindings & body]
|
[bindings & body]
|
||||||
(def head (ast-unwrap1 bindings))
|
(tuple 'let bindings
|
||||||
(tuple 'let head
|
|
||||||
(tuple
|
(tuple
|
||||||
'when
|
'when
|
||||||
(tuple 'and (tuple 'get bindings 1)
|
(tuple 'and (tuple 'get bindings 1)
|
||||||
|
Loading…
Reference in New Issue
Block a user