Skip to content

Add the assoc-in-some function#72

Closed
isapim wants to merge 1 commit intoweavejester:masterfrom
isapim:assoc-in-some
Closed

Add the assoc-in-some function#72
isapim wants to merge 1 commit intoweavejester:masterfrom
isapim:assoc-in-some

Conversation

@isapim
Copy link

@isapim isapim commented Feb 27, 2023

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

A function that behaves similarly to assoc-some, but for nested keys.
@weavejester
Copy link
Owner

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))

@isapim
Copy link
Author

isapim commented Feb 28, 2023

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
Copy link
Owner

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))

@isapim
Copy link
Author

isapim commented Feb 28, 2023

Yeah, it makes sense. Thank you!

@isapim isapim closed this Feb 28, 2023
@isapim isapim deleted the assoc-in-some branch February 28, 2023 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants