Hi all,

I have a sequence of {:key K :value V} maps.  There is no guarantee that 
there is one map for every known K.  I need to ensure that there is a map 
for each known K.

Imagine the sequence represents a distribution between 1 and 5.  The 
initial sequence might only be [{:key 3 :value 30} {:key 4 :value 40}].  I 
want it to be [{:key 1 :value nil} {:key 2 :value nil} {:key 3 :value 30} 
{:key 4 :value 40} {:key 5 :value nil}].

(There is a sensible reason for {:key K :value V} rather than {K V} by the 
way, but it would be possible to temporarily represent the sequence as [K1 
V1 K2 V2] and then convert it back.)

In Java I would do something like:

// create a convenient look up to avoid nasty N^2 lookups
Map<Object, Object> keyToValue = new HashMap....
for (Map<Object, Object> kvMap: kvSequence) 
  keyToValue.put(kvMap.get("key"), kvMap.put("value"));

List<Object> allKeys = calculateAllKeys();

List<Map<Object, Object>> results = new Array....
for (Object key: allKeys)
  Object result  = keyToValue.get(key); // null is fine for missing keys
  results.put(key, result);

return results;
    
What is the (far more elegant and succinct!) way in Clojure?

My intuition says something like create a map of K/V (i.e. {k1 nil k2 nil}) 
for all known keys, convert the existing sequence of maps into a flattened 
map, merge them together allowing the original sequence to take precedence 
and then group them back into [{:key k1 :value v1} {:key k2 :value v2}].

Any hints/clues - is this the right approach?

Thanks a bunch!

Col


-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to