Something like this, perhaps:

    (assoc m k (conj (m k []) v))

Where m is the map, k is the key, and v is the new value you want to
append. This works because (m k []) will default to [] if the key k isn't
in the map m.

Usually if a key cannot be found, it defaults to nil. If you don't care so
much about order, you can take advantage of the fact that (conj nil 1) is
equal to '(1). So we could write:

    (assoc m k (conj (m k) v))

Which can also be written using update-in:

    (update-in m [k] conj v)

However, because lists conjoin elements to the front of the list, newer
values will be at the front, rather than the back.

- James


On 17 June 2014 18:29, Hussein B. <[email protected]> wrote:

> Please keep in mind that I'm starting from an empty map. So, I'm looking
> for an idiomatic Clojure code to achieve my purpose where if the map
> doesn't has that specific key, it will create an entry of that key and add
> the value to the newly created vector. And if the key exists, then add the
> value to the existing vector.
>
>
> On Tuesday, June 17, 2014 7:21:43 PM UTC+2, Hussein B. wrote:
>>
>> Hi,
>>
>> This is a very basic question, so be patient please! :)
>>
>> I have an empty map where key is an integer and the value is a vector.
>>
>> How I can add an element to the vector of a specific key?
>>
>> For example:
>>
>> {1 [11]}
>>
>> Then
>>
>> {1 [ 11 22]}
>>
>> Thanks for help and time.
>>
>  --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to