Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8082875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:15:54+00:00 2026-06-05T17:15:54+00:00

Very simple + silly question: Does clojure provide multi maps? I currently have something

  • 0

Very simple + silly question:

Does clojure provide multi maps? I currently have something like this:

(defn wrap [func]
  (fn [mp x]
    (let [k (func x)]
      (assoc mp k
             (match (get mp k)
               nil [x]
               v (cons v x))))))

(defn create-mm [func lst]
  (reduce (wrap func) {} lst))

Which ends up creating a map, where for each key, we have a vector of all elements with that key. However, it seems like multi maps is a very basic data structure, and I wonder if clojure has it built-in.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:15:55+00:00Added an answer on June 5, 2026 at 5:15 pm

    I don’t think this is really necessary as a distinct type, as Clojure’s flexibility allow you to quickly make your own by just using maps and sets. See here:

    http://paste.lisp.org/display/89840

    Edit (I should have just pasted this in since it’s so small)

    Example Code (Courtesy Stuart Sierra)

    (ns #^{:doc "A multimap is a map that permits multiple values for each
      key.  In Clojure we can represent a multimap as a map with sets as
      values."}
      multimap
      (:use [clojure.set :only (union)]))
    
    (defn add
      "Adds key-value pairs the multimap."
      ([mm k v]
         (assoc mm k (conj (get mm k #{}) v)))
      ([mm k v & kvs]
         (apply add (add mm k v) kvs)))
    
    (defn del
      "Removes key-value pairs from the multimap."
      ([mm k v]
         (let [mmv (disj (get mm k) v)]
           (if (seq mmv)
             (assoc mm k mmv)
             (dissoc mm k))))
      ([mm k v & kvs]
         (apply del (del mm k v) kvs)))
    
    (defn mm-merge
      "Merges the multimaps, taking the union of values."
      [& mms]
      (apply (partial merge-with union) mms))
    
    (comment
      (def mm (add {} :foo 1 :foo 2 :foo 3))
      ;; mm == {:foo #{1 2 3}}
    
      (mm-merge mm (add {} :foo 4 :bar 2))
      ;;=> {:bar #{2}, :foo #{1 2 3 4}}
    
      (del mm :foo 2)
      ;;=> {:foo #{1 3}}
    )
    

    Extra test for the case pointed out in the comments:

    (comment
      (-> {} (add :a 1) (del :a 1) (contains? :a))
      ;;=> false
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem a very silly question. Consider this: I have a simple Boolean
I have a very simple table called genre , which looks like this: GENRE
This is probably a very silly and simple question to most of you, but
This is my first question, probably very silly indeed :) I have a selection
This is very simple.I am sure I am missing something silly. fp = open(r'D:\UserManagement\invitationTemplate.html',
This probably seems like a very simple question, and I would probably know if
Very simple question but I'm quite new to JS and this is not easy
Very simple and probably noob question about php. i run the following; var_dump($this->criteria); which
Very simple question. What does the term 'seeding' mean in general? I'll put the
Okay, this is (probably) a very simple question, but I am afraid I know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.