weavejester / medley

A lightweight library of useful Clojure functions
Eclipse Public License 1.0
865 stars 66 forks source link

Add the assoc-in-some function #72

Closed ipimentelmoraes closed 1 year ago

ipimentelmoraes commented 1 year ago

A function that behaves similarly to assoc-some, but for nested keys. Works based on clojure.core/assoc-in.

weavejester commented 1 year ago

My initial thought is that this function doesn't have a broad enough use-case for its relative simplicity. For example, you could write instead:

(some->> v (assoc-in m ks))
;; same as
(assoc-in-some m ks v)

assoc-some is a little more complex, as there may be multiple key/value pairs that are checked:


(assoc-some m k1 v1 k2 v2 k3 v3)
;; same as
(cond-> m v1 (assoc k1 v1) v2 (assoc k2 v2) v3 (assoc k3 v3))
ipimentelmoraes commented 1 year ago

Yeah, it really is simple. But in our scenario having it on a separate function would be helpful when threading and checking different values. Just an example:

(-> request-input
      (assoc-in-some [:headers "x-180s-correlation-id"] cid)
      (assoc-in-some [:headers "x-180s-forwarded-ids"] forwarded-ids))

But if you think medley is not the best place to keep this function, no problem at all :)

weavejester commented 1 year ago

This would also work, no?

(-> request-input
    (update :headers assoc-some "x-180s-correlation-id" cid)
    (update :headers assoc-some "x-180s-forwarded-ids" forwarded-ids))
ipimentelmoraes commented 1 year ago

Yeah, it makes sense. Thank you!