Re: Key order of literal maps

2011-05-03 Thread David Jagoe
Thanks for the responses.

On 3 May 2011 17:39, Steve Miner  wrote:
>
> On May 3, 2011, at 7:08 AM, David Jagoe wrote:
>
> For your specific purpose, I would be careful about using a map as an 
> "entity" specification. If the order is significant, a vector of field 
> specifiers would be better.  Instead of taking a map as the second argument 
> to defentity, you could make it variadic like (defmacro defentity [ent & 
> specs] ...).

Yes, that looks like the solution.


Thanks again.

David

>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> 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 post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Key order of literal maps

2011-05-03 Thread Steve Miner

On May 3, 2011, at 7:08 AM, David Jagoe wrote:

> Can I rely on (keys some-literal-map) always returning the keys in the
> order they were defined in the literal map?

In general, the key order is not guaranteed, but an array-map will maintain the 
insertion order of the keys.  Use the array-map function to create one. There's 
a bit more info here:

http://clojuredocs.org/clojure_core/clojure.core/array-map

> An array-map maintains the insertion order of the keys. Look up is linear, 
> which is not a problem for small maps (say less than 10 keys). If your map is 
> large, you should use hash-map instead.
> 
> When you assoc onto an existing array-map, the result is a new array-map with 
> the new key as the first key. The rest of the keys are in the same order as 
> the original. Functions such as seq and keys will respect the key order.
> 
> Note that assoc will decide to return a hash-map if the result is too big to 
> be efficient.

For your specific purpose, I would be careful about using a map as an "entity" 
specification. If the order is significant, a vector of field specifiers would 
be better.  Instead of taking a map as the second argument to defentity, you 
could make it variadic like (defmacro defentity [ent & specs] ...).

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Key order of literal maps

2011-05-03 Thread Armando Blancas
Keys from literal maps aren't sorted; you need a sorted map.

user=> (keys {:z 1 :f 2 :a 0})
(:z :a :f)
user=> (keys (sorted-map :z 1 :f 2 :a 0))
(:a :f :z)

On May 3, 4:08 am, David Jagoe  wrote:
> Hey everyone,
>
> I'm busy implementing a macro whose usage looks like this:
>
> (defentity Person
>   {:name      {:type String :validator name-validator}
>    :id-number {:type String :validator id-number-validator}
>    :height    {:type Float  :default 0.0}
>    :weight    {:type Float  :default 0.0}
>    :bmi       {:type Float  :internal true}})
>
> The macro generates a defrecord like so:
>
> (defrecord Person [name id-number height weight bmi])
>
> Which can later be used in the application like this:
>
> (Person. "David" "123" 1 2 3)
>
> Obviously the argument order is important. So my question is:
>
> Can I rely on (keys some-literal-map) always returning the keys in the
> order they were defined in the literal map? In my experiment, a map
> literal will create a PersistentArrayMap which I understand has sorted
> keys. Can someone confirm that this is reliable (e.g. across
> implementations?)
>
> Thanks!
> David

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Key order of literal maps

2011-05-03 Thread David Jagoe
Hey everyone,

I'm busy implementing a macro whose usage looks like this:

(defentity Person
  {:name  {:type String :validator name-validator}
   :id-number {:type String :validator id-number-validator}
   :height{:type Float  :default 0.0}
   :weight{:type Float  :default 0.0}
   :bmi   {:type Float  :internal true}})

The macro generates a defrecord like so:

(defrecord Person [name id-number height weight bmi])

Which can later be used in the application like this:

(Person. "David" "123" 1 2 3)

Obviously the argument order is important. So my question is:

Can I rely on (keys some-literal-map) always returning the keys in the
order they were defined in the literal map? In my experiment, a map
literal will create a PersistentArrayMap which I understand has sorted
keys. Can someone confirm that this is reliable (e.g. across
implementations?)


Thanks!
David

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en