Map from generator?

2 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Conrad

ungelesen,
04.02.2009, 22:36:3504.02.09
an Clojure
It is useful to build a map from a list of keys and a value generator
function. Of Course, such a function is easy to write:

(defn genmap [keys fun]
(zipmap keys (map fun keys)))

In fact, it seems so useful that it must be in the standard API
somewhere, but I can't find it... did I miss it somewhere, or was I
right to create my own? Please let me know if I'm reinventing the
wheel.

Thanks!

Conrad Barski

Rich Hickey

ungelesen,
05.02.2009, 07:33:4905.02.09
an Clojure
It does seem useful, although the zipmap call is already concise.

I wonder if this is a frequently reinvented wheel.

Rich

Mark McGranaghan

ungelesen,
05.02.2009, 07:46:3005.02.09
an clo...@googlegroups.com
I frequently use a more general version of this function that reduces
a seq to a map, mapping each element in the seq to a [key value] pair
for the map. I use this in several different libs:

(defn mash
"Reduce a seq-able to a map. The given fn should return a 2-element tuple
representing a key and value in the new map."
[f coll]
(reduce
(fn [memo elem]
(let [[k v] (f elem)]
(assoc memo k v)))
{} coll))

Where "mash" comes from map + hash.

(mash (fn [elem] [(+ 1 elem) (+ 2 elem)]) (list 1 2 3))
=> {4 5, 3 4, 2 3}

You could also use into:

(into {} (map (fn [elem] [(+ 1 elem) (+ 2 elem)]) (list 1 2 3)))
=> {4 5, 3 4, 2 3}

- Mark

Albert Cardona

ungelesen,
06.02.2009, 04:47:3206.02.09
an clo...@googlegroups.com


I had an almost identical (if less elegant) function to "genmap"
as well in my files. Adding it to core would be nice.

Albert

--
Albert Cardona
http://albert.rierol.net

Jason Wolfe

ungelesen,
06.02.2009, 13:04:0306.02.09
an Clojure
> (defn mash
>   "Reduce a seq-able to a map. The given fn should return a 2-element tuple
>   representing a key and value in the new map."
>   [f coll]
>   (reduce
>     (fn [memo elem]
>       (let [[k v] (f elem)]
>         (assoc memo k v)))
>     {} coll))

I called this "map-map" in my utilities. Mine was even a bit more
general, in that it could take multiple collections like "map". This
allowed you to say thinks like

(map-map vector s (iterate inc 0))

to get a map from elements of s to indices.

Then Chouser pointed out [1] that map-map is equivalent to just

(into {} (map f coll1 coll2 ...))

[1] http://groups.google.com/group/clojure/browse_thread/thread/134642cc76de17f7/b93b74fa4a6806cd?hl=en&q=utilities&lnk=ol&

I still kind of like map-map, although I'm not sure if it's really any
better than the idiom Chouser gave. YMMV, but I'm pretty sure the
consensus was that map-map didn't belong in core, or probably even
contrib.

-Jason
Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten