Re: Padding missing elements in a sequence of identical K/V maps

2013-11-20 Thread Colin Yates
Thanks all. This just highlights one of the nice things about Clojure to me - the code gets out of the way to allow the algorithm to shine through. Lovely. By the way, this implementation is closest to my initial although I didn't use juxt at the repl, but I did lose my repl history and can't

Re: Padding missing elements in a sequence of identical K/V maps

2013-11-19 Thread Kelker Ryan
Maybe this? user> (defn ensure-mandatory " coll - keys of required maps maps - collection of maps" [coll maps] (loop [adding (clojure.set/difference (set coll) (->> maps (map keys) flatten set)) all maps

Re: Padding missing elements in a sequence of identical K/V maps

2013-11-19 Thread Kelker Ryan
Maybe this? user> (defn ensure-mandatory " coll - keys of required maps maps - collection of maps" [coll maps] (loop [adding (clojure.set/difference (set coll) (->> maps (map keys) flatten set)) all map

Re: Padding missing elements in a sequence of identical K/V maps

2013-11-19 Thread John Mastro
This was my first thought (quite close to Jim's): (def the-maps [{:key 3 :value 30} {:key 4 :value 40}]) (def mandatory-keys [1 2 3 4 5]) (defn find-missing-keys [maps keys] (let [found (into #{} (map :key maps))] (remove #(contains? found %) keys))) (defn ensure-mandatory-keys [maps] (

Re: Padding missing elements in a sequence of identical K/V maps

2013-11-19 Thread Joshua Ballanco
In an attempt to be slightly more "elegant" (whatever that means ;-) ): -8<-8<- (def start [{:key 3 :value 10} {:key 6 :value 30}]) (into [] (map (fn [[k v]] {:key k :value v}) (merge (into {} (for [x (range 6)] {x nil})) (into {} (map (juxt :key :value) start) ;;=> [{:key 6, :

Re: Padding missing elements in a sequence of identical K/V maps

2013-11-19 Thread Jim - FooBar();
On 19/11/13 11:29, Colin Yates wrote: In Java I would do something like: // create a convenient look up to avoid nasty N^2 lookups Map keyToValue = new HashMap for (Map kvMap: kvSequence) keyToValue.put(kvMap.get("key"), kvMap.put("value")); List allKeys = calculateAllKeys(); List> resul

Re: Padding missing elements in a sequence of identical K/V maps

2013-11-19 Thread Colin Yates
Great stuff - thanks Jim. (not sure why my previous post got lost) On Tuesday, November 19, 2013 11:48:17 AM UTC, Jim foo.bar wrote: > > On 19/11/13 11:42, Jim - FooBar(); wrote: > > On 19/11/13 11:29, Colin Yates wrote: > >> Imagine the sequence represents a distribution between 1 and 5. The

Re: Padding missing elements in a sequence of identical K/V maps

2013-11-19 Thread Jim - FooBar();
On 19/11/13 11:42, Jim - FooBar(); wrote: On 19/11/13 11:29, Colin Yates wrote: 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

Re: Padding missing elements in a sequence of identical K/V maps

2013-11-19 Thread Jim - FooBar();
On 19/11/13 11:29, Colin Yates wrote: 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}]