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
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
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
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]
(
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, :
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
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
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
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}]