diff --git a/README.md b/README.md index ec29883..a4e1b6b 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Transducing context: `transjuxt` (for performing several transductions in a sing Add this dependency to your project: ```clj -[net.cgrand/xforms "0.1.0"] +[net.cgrand/xforms "0.1.1-SNAPSHOT"] ``` ```clj @@ -145,7 +145,7 @@ My faithful `(reduce-by kf f init coll)` is now `(into {} (x/by-key kf (x/reduce ## License -Copyright © 2015 Christophe Grand +Copyright © 2015-2016 Christophe Grand Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version. diff --git a/project.clj b/project.clj index 1c2493b..608646e 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject net.cgrand/xforms "0.1.0" +(defproject net.cgrand/xforms "0.1.1-SNAPSHOT" :description "Extra transducers for Clojure" #_#_:url "http://example.com/FIXME" :license {:name "Eclipse Public License" diff --git a/src/net/cgrand/xforms.clj b/src/net/cgrand/xforms.clj index dff3b6b..44bb5fc 100644 --- a/src/net/cgrand/xforms.clj +++ b/src/net/cgrand/xforms.clj @@ -307,15 +307,54 @@ ([_ x] (reduced x))) (defn transjuxt - "Performs several transductions over coll at once. xforms-map can be a map or vector. - Returns a map with the same keyset as xforms-map (or a vector of same size as xforms-map). + "Performs several transductions over coll at once. xforms-map can be a map or a sequential collection. + When xforms-map is a map, returns a map with the same keyset as xforms-map. + When xforms-map is a sequential collection returns a vector of same length as xforms-map. Returns a transducer when coll is omitted." ([xforms-map] - (let [[f args] (if (vector? xforms-map) - [juxt (map #(% first))] - [juxt-map (comp (by-key (map #(% first))) cat)])] + (let [[f args] (if (map? xforms-map) + [juxt-map (comp (by-key (map #(% first))) cat)] + [juxt (map #(% first))])] (fn [rf] ((reduce (apply f (sequence args xforms-map))) rf)))) ([xforms-map coll] (transduce (transjuxt xforms-map) first coll))) +#_(defn intermix + [xforms] + (fn [rf] + (let [mxrf (multiplexable rf) + rfs (volatile! (into #{} (map #(%2 mxrf)) xforms))] + (fn + ))) + ) + +(defn tag + "Like (map #(vector tag %)) but potentially more efficient." + [tag] + (fn [rf] + (if-some [rf (some-kvrf rf)] + (fn + ([] (rf)) + ([acc] (rf acc)) + ([acc v] (rf acc tag v))) + (fn + ([] (rf)) + ([acc] (rf acc)) + ([acc v] (rf acc [tag v])))))) + +(defn map-kv + "Like (map (fn [[k v]] [(kf k v) (vf k v)])) but potentially more efficient." + [kf vf] + (fn [rf] + (if-some [rf (some-kvrf rf)] + (kvrf + ([] (rf)) + ([acc] (rf acc)) + ([acc [k v]] (rf acc (kf k v) (vf k v))) + ([acc k v] (rf acc (kf k v) (vf k v)))) + (kvrf + ([] (rf)) + ([acc] (rf acc)) + ([acc [k v]] (rf acc [(kf k v) (vf k v)])) + ([acc k v] (rf acc [(kf k v) (vf k v)])))))) \ No newline at end of file