I've been using a number of similar functions in my own coding.  This
was my approach.

(defn seq2map
  "Constructs a map from a sequence by applying keyvalfn to each
   element of the sequence.  keyvalfn should return a pair [key val]
   to be added to the map for each input sequence element."
  [aseq keyvalfn]
  (loop [aseq aseq
         amap {}]
    (if (empty? aseq)
      amap
      (let [[key val] (keyvalfn (first aseq))]
        (recur (rest aseq)
               (assoc amap key val))))))

(defn seq2redundant-map
  "Constructs a map from a sequence by applying keyvalfn to each
   element of the sequence.  keyvalfn should return a pair [key val]
   to be added to the map for each input sequence element.  If key is
   already in the map, its current value will be combined with the new
   val using (mergefn curval val)."
  [aseq keyvalfn mergefn]
  (loop [aseq aseq
         amap {}]
    (if (empty? aseq)
      amap
      (let [[key val] (keyvalfn (first aseq))]
        (recur (rest aseq)
               (update-in amap [key] mergefn val))))))

(defn maphash
  "Creates a new map by applying keyfn to every key of in-map and
   valfn to every corresponding val."
  [keyfn valfn in-map]
  (seq2map (seq in-map) (fn [[key val]] [(keyfn key) (valfn val)])))

 ~Gary

-- 
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

Reply via email to