Édipo Féderle
Édipo Féderle

Reputation: 4257

Clojure update-in on two keys and two functions

I am trying to update a nested hashmap using update-in function. But I want to update value of two keys, using differents functions. For exemple:

I have this hash:

{:1 {:value 0, :active false}, :2 {:value 0, :active false}

And I want update the key :1 to:

{:1 {:value 2, :active true}, :2 {:value 0, :active false}

There is some way to do this ?

Thanks in advance

Update Maybe I just can use assoc: (assoc my-map :1 {:value 2, :active true})

Upvotes: 0

Views: 100

Answers (2)

cfrick
cfrick

Reputation: 37008

You can have more than one k/v pair with assoc:

user=> (def m {:1 {:value 0, :active false}, :2 {:value 0, :active false}})
#'user/m
user=> (update-in m [:1] assoc :value 1 :active true)
{:1 {:value 1, :active true}, :2 {:value 0, :active false}}

Upvotes: 2

user5187212
user5187212

Reputation: 426

There are also assoc-in which works like assoc. The only difference is, that you provide a vector of keys instead of a single key. So maybe you can pipe your map through some assoc-in's.

Or you use the function update (added in 1.7):

(update {:1 {:value 0, :active false}, :2 {:value 0, :active false}
    :1 (fn [{:keys [value active]]
           (magic value active)))

Upvotes: 1

Related Questions