Search code examples
dictionaryvectorclojurezipmap

Combine and do calculation two vectors return map Clojure


I got two vectors, [shoes milk shoes] and [1 3 1], and the map I want to get is {shoes 2, milk 3}. I tried to zipmap two vectors and only {shoes 1 milk 3} shows. Without loop and iterate, is there another way to do that?


Solution

  • you can also employ a bit different solution for that, generating one-entry maps for item-to-amount pair, and then merging them with +:

    (let [goods '[shoes milk shoes]
          amounts [1 3 1]]
      (apply merge-with + (map hash-map goods amounts)))
    
    ;;=> {milk 3, shoes 2}