Welcome! Please see the About page for a little more info on how this works.
(let [coll [{:a 1 :b 2} {:a 3 :b 4} {:a 7 :b 5} {:a 1 :b 4}] xf1 (comp (map :a) (filter #{1 3})) xf2 (comp (map :b) (filter #{2 4}))] (concat (into [] xf1 coll) (into [] xf2 coll)))
is there a way to combine the two xforms above (xf1 and xf2) into a single xform somehow so the collection coll will be traversed only once?
not really a combination of transducers, but:
(let [coll [{:a 1 :b 2} {:a 3 :b 4} {:a 7 :b 5} {:a 1 :b 4}] xf1 #(-> % :a #{1 3}) xf2 #(-> % :b #{2 4})] (into [] (comp (mapcat (juxt xf1 xf2)) (remove nil?)) coll)) => [1 2 3 4 1 4]
(let [coll [{:a 1 :b 2} {:a 3 :b 4} {:a 7 :b 5} {:a 1 :b 4}] xf1 (comp (map :a) (filter #{1 3})) xf2 (comp (map :b) (filter #{2 4}))] (eduction cat [(eduction xf1 coll) (eduction xf2 coll)]))
eduction can be replaced with sequence
eduction
sequence
(defn facet [m] (fn [f] (let [m (into {} (for [[k v] m] [k (v f)]))] (fn ([accum] (reduce (fn [accum1 [k accum2]] (assoc accum1 k ((get m k) accum2))) accum accum)) ([accum value] (reduce (fn [accum1 [k accum2]] (assoc accum1 k ((get m k) accum2 value))) accum accum)))))) (let [coll [{:a 1 :b 2} {:a 3 :b 4} {:a 7 :b 5} {:a 1 :b 4}] xf1 (comp (map :a) (filter #{1 3})) xf2 (comp (map :b) (filter #{2 4}))] ((comp (partial apply concat) (juxt :a :b)) (transduce (facet {:a xf1 :b xf1}) conj {:a [] :b []} coll)))