Oups, indeed this doesn't solve the problem, this other solution works (just
a variant of Kresimir's one) :
(reduce (fn [m [k v]] (update-in m [k] #(conj (or %1 []) %2) v)) {} [[:a 1]
[:b 3] [:b 5] [:c 1]])
2009/6/24 Laurent PETIT
> Does this fit your need ? :
>
> (reduce (fn [m [k v]] (assoc m
Have you tried zipmap?
Emeka
On Tue, Jun 23, 2009 at 10:09 PM, samppi wrote:
>
> The idiom (into {} coll-of-entries) is often used to create a map from
> a collection of entries or two-sized vectors. But what if I want to do
> something like this:
>
> (mystery-fn [[:a 1] [:b 3] [:b 5] [:c 1]])
Hello,
other solutions:
(apply hash-map (apply concat [[:a 1] [:b 3] [:e 5] [:c 1]]))
or
(apply hash-map (mapcat identity [[:a 1] [:b 3] [:e 5] [:c 1]]))
I didn't find any function equivalent to "apply concat".
Is there something like a monadic join function for sequence in the API?
Best reg
Thanks everyone. They all seem to take less time than the filter way
too.
On Jun 23, 4:05 pm, Cosmin Stejerean wrote:
> On Tue, Jun 23, 2009 at 5:09 PM, samppi wrote:
>
> > The idiom (into {} coll-of-entries) is often used to create a map from
> > a collection of entries or two-sized vectors. B
On Tue, Jun 23, 2009 at 5:09 PM, samppi wrote:
>
> The idiom (into {} coll-of-entries) is often used to create a map from
> a collection of entries or two-sized vectors. But what if I want to do
> something like this:
>
> (mystery-fn [[:a 1] [:b 3] [:b 5] [:c 1]]) ; returns {:a [1], :b [3
> 5], :
Hi,
Try:
(def x [[:a 1] [:b 3] [:b 5] [:c 1]])
(into {} (map (fn [[k v]] [k [v]]) a))
Returns:
{:c [1], :b [5], :a [1]}
--
Krešimir Šojat
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to t
Come to think of it, this would also work for me: keeping the vector
of pairs, and instead using filter to get the "values" of a key:
(defn get-from-pairs [pairs key-to-fetch]
(map #(get % 1) (filter #(= key-to-fetch (get % 0)) pairs)))
(I wish the key and val functions were defined on vectors
Does this fit your need ? :
(reduce (fn [m [k v]] (assoc m k [v])) {} [[:a 1] [:b 3] [:b 5] [:c 1]])
HTH,
--
Laurent
2009/6/24 samppi
>
> The idiom (into {} coll-of-entries) is often used to create a map from
> a collection of entries or two-sized vectors. But what if I want to do
> somethin
Sorry about that, didn't see that :b should concat it's vals:
(def x [[:a 1] [:b 3] [:b 5] [:c 1]])
(reduce (fn [x [k v]] (assoc x k (if (contains? x k) (conj (x k) v)
[v]))) {} x)
Returns:
{:c [1], :b [3 5], :a [1]}
--
Krešimir Šojat
--~--~-~--~~~---~--~~
You re
The idiom (into {} coll-of-entries) is often used to create a map from
a collection of entries or two-sized vectors. But what if I want to do
something like this:
(mystery-fn [[:a 1] [:b 3] [:b 5] [:c 1]]) ; returns {:a [1], :b [3
5], :c [1]})
The only way I can think of doing this is with a com
10 matches
Mail list logo