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 8110849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:54:17+00:00 2026-06-06T01:54:17+00:00

To preface, I am on Windows 7 (64-bit), running Java version 6 (update 33)

  • 0

To preface, I am on Windows 7 (64-bit), running Java version 6 (update 33) using clooj as my IDE. I have not tried to reproduce my problem in any other system. I am experienced with Clojure, but not at all with Java.

The entirety of the problem I am trying to solve is lengthy to describe, but it boils down to this: let’s say I would like to a make a macro that takes one argument, an associative map, and returns a vector of the elements of the map with their order conserved.

=>(defmacro vectorize-a-map
    [associative-map]
    (vec associative-map))
=>#'ns/vectorize-a-map
=>(vectorize-a-map {:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8}
=>[[:a 1] [:b 2] [:c 3] [:d 4] [:e 5] [:f 6] [:g 7] [:h 8]]

That works, but add another element to the map and the order messes up…

=>(vectorize-a-map {:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8 :i 9}
=>[[:a 1] [:c 3] [:b 2] [:f 6] [:g 7] [:d 4] [:e 5] [:i 9] [:h 8]]

I believe I have discovered why this is happening. It seems like anything with 8 or fewer elements is instantiated as a PersistentArrayMap, which is exactly what I want, because from what I can tell, this class retains order. However, anything with 9 or more elements is instantiated as a PersistentHashMap, which does not retain order.

=>(type {:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8}
=>clojure.lang.PersistentArrayMap
=>(type {:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8 :i 9}
=>clojure.lang.PersistentHashMap

I would like my macro to be able to take associative maps of any size, so this is a problem. I have tried type hinting, destructuring binding, for list comprehension, and unquote splicing, all without success. To draw it out, none of the following will work:

(defmacro vectorize-a-map
  [^clojure.lang.PersistentArrayMap associative-map]
  (vec associative-map))

(defmacro vectorize-a-map
  [[& associative-map]]
  (vec associative-map))

(defmacro vectorize-a-map
  [associative-map]
  (vec
    (for [x associative-map]
      x)))

(defmacro vectorize-a-map
  [associative-map]
  `(vector ~@associative-map))

With this toy problem I present, I realize I could simply write my macro like so, and avoid the problem altogether:

=>(defmacro vectorize-kvs
    [& elements]
    (vec (map vec (partition 2 elements))))
=>#'ns/vectorize-kvs
=>(vectorize-kvs :a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8 :i 9)
=>[[:a 1] [:b 2] [:c 3] [:d 4] [:e 5] [:f 6] [:g 7] [:h 8] [:i 9]]

However, for the actual problem I am trying to solve (which I have not gotten into), it is important (although not 100% necessary) that the macro be able to take associative maps. It seems like I’m looking for how to cast the argument into a PersistentArrayMap before anything has a chance to happen to it. There may be some other avenue to a solution that I am simply not considering or aware of.

I’ve researched the best I’ve known how and haven’t found anything helpful yet. Does anybody have any thoughts/advice?

  • 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-06T01:54:19+00:00Added an answer on June 6, 2026 at 1:54 am

    you can make your map with array-map

    user> (map vec (array-map 1 2 3 4 5 6))
    ([1 2] [3 4] [5 6])
    

    or with a larger map

    user> (map vec (apply array-map (range 50)))
    ([0 1] [2 3] [4 5] [6 7] [8 9] [10 11] [12 13] [14 15] [16 17] [18 19] [20 21] [22 23] [24 25] [26 27] [28 29] [30 31] [32 33] [34 35] [36 37] [38 39] [40 41] [42 43] [44 45] [46 47] [48 49])
    

    as a bonus you can avoid using a macro, which is useful because macros are not first-class and don’t compose well*


    a note on your first comment from the documentation on array map

     Note that an array map will only maintain sort order when un-'modified'. 
     Subsequent assoc-ing will eventually cause it to 'become' a hash-map.

    If you find yourself depending on the order of keys in your maps you may want to consider if a sorted-map will get you what you need. It will scale better than an array-map. In the above example the output is the same:

    (map vec (apply sorted-map (range 5000)))
    [0 1] [2 3] ... [4998 4999]
    

    *this is my opinion


    EDIT:

    a time comparason of sorted-map vs. array-map

    user> (time (dorun (map vec (apply sorted-map (range 500000)))))
    "Elapsed time: 391.520491 msecs"
    nil
    user> (time (dorun (map vec (apply array-map (range 500000)))))
    "Elapsed time: 674517.821669 msecs"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Preface: I have a demo of the problem on my personal site (I hope
Preface: I have a MS Access 2010 front end running against a MySQL back
Preface: This is not much about how to structure code within files. I have
Preface: I have a broad, college knowledge, of a handful of languages (C++, VB,C#,Java,
Preface: I am not using *.xib files. I instantiate a UINavigationController in a class
Preface: I'm a total Java noob...I just wrote Hello World yesterday. Have mercy on
To preface this, I do not work with Java or Struts, but I understand
Let me preface this problem with the following: I've only tested this using the
Preface : I'm honestly not sure if this should be on StackOverflow, SuperUser or
Preface: I have two entities defined as a one-to-many relationship: A <<-------> B. B's

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.