Open erkin opened 3 years ago
This is the naïve implementation I use in my projects:
(define (dict-ref-in dict keys (not-found #f))
(if (null? keys)
dict
(dict-ref-in (dict-ref dict (car keys)) (cdr keys) not-found)))
(define (dict-update-in dict keys function . args)
(dict-update dict (car keys)
(if (null? (cdr keys))
(λ (v) (apply function v args))
(λ (v) (apply dict-update-in v (cdr keys) function args)))))
(define (dict-set-in dict keys value)
(dict-update-in dict keys (thunk* value)))
Although I'm not actively working on rackjure
lately (ergo this slow reply):
I just wanted to point out that it arranges for Racket dictionaries to be applicable -- which enables using the threading macro ~>
to flatten nested references like that. See https://docs.racket-lang.org/rackjure/index.html#%28part._dict-app%29.
Derp. Just after posting that I realize you're talking about updating.
I must say I love rackjure but there's something that's dearly missing for me. In Clojure you can use
update-in
to update nested maps (which are very common when doing state-based programming), such asThis is very burdensome to do with Racket, such as
I think something like a
dict-update-in
would be a great addition to rackjure.Likewise
dict-ref-in
(get-in
),dict-set-in
(assoc-in
) anddict-remove-in
(dissoc-in
).