Warm tip: This article is reproduced from serverfault.com, please click

Combine transduction output with input into a hashmap

发布于 2020-12-01 10:27:37

I want to do the following in Clojure as idiomatically as possible:

  1. transduce a collection
  2. associate each element of the input collection with the corresponding element in the output collection
  3. return the result in a hashmap

Is there a succinct way to do this using core library functions?

If not, what improvements can you suggest to the following implementation?

(defn to-hash [coll xform]
  (reduce
    merge
    (map
      #(apply hash-map %)
      (mapcat hash-map coll (into [] xform coll)))))
Questioner
Eureton
Viewed
0
leetwinski 2020-12-01 19:50:18

something like this should do the trick without intermediate collections:

(defn process [data xform]
  (zipmap data (eduction xform data)))

user> (process [1 2 3] (comp (map inc) (map #(* % %))))
;;=> {1 4, 2 9, 3 16}

the docs on eduction say the following:

Returns a reducible/iterable application of the transducers to the items in coll. Transducers are applied in order as if combined with comp. Note that these applications will be performed every time reduce/iterator is called.

so no additional collection is created.

This is any good, of course, as long as there is one-to-one relationship between input and output elements. What is desired output for (process [1 -2 3] (filter pos?)) or (process [1 1 1 2 2 2] (dedupe)) ?

(by the way, your to-hash implementation has the same flaw)