Makes transjxut accepts any sequential collection (and not just vectors). Bump to 0.1.1

This commit is contained in:
Christophe Grand 2016-04-12 14:20:22 +02:00
parent 560f209914
commit 45af15c69d
3 changed files with 47 additions and 8 deletions

View file

@ -15,7 +15,7 @@ Transducing context: `transjuxt` (for performing several transductions in a sing
Add this dependency to your project: Add this dependency to your project:
```clj ```clj
[net.cgrand/xforms "0.1.0"] [net.cgrand/xforms "0.1.1-SNAPSHOT"]
``` ```
```clj ```clj
@ -145,7 +145,7 @@ My faithful `(reduce-by kf f init coll)` is now `(into {} (x/by-key kf (x/reduce
## License ## License
Copyright © 2015 Christophe Grand Copyright © 2015-2016 Christophe Grand
Distributed under the Eclipse Public License either version 1.0 or (at Distributed under the Eclipse Public License either version 1.0 or (at
your option) any later version. your option) any later version.

View file

@ -1,4 +1,4 @@
(defproject net.cgrand/xforms "0.1.0" (defproject net.cgrand/xforms "0.1.1-SNAPSHOT"
:description "Extra transducers for Clojure" :description "Extra transducers for Clojure"
#_#_:url "http://example.com/FIXME" #_#_:url "http://example.com/FIXME"
:license {:name "Eclipse Public License" :license {:name "Eclipse Public License"

View file

@ -307,15 +307,54 @@
([_ x] (reduced x))) ([_ x] (reduced x)))
(defn transjuxt (defn transjuxt
"Performs several transductions over coll at once. xforms-map can be a map or vector. "Performs several transductions over coll at once. xforms-map can be a map or a sequential collection.
Returns a map with the same keyset as xforms-map (or a vector of same size as xforms-map). 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." Returns a transducer when coll is omitted."
([xforms-map] ([xforms-map]
(let [[f args] (if (vector? xforms-map) (let [[f args] (if (map? xforms-map)
[juxt (map #(% first))] [juxt-map (comp (by-key (map #(% first))) cat)]
[juxt-map (comp (by-key (map #(% first))) cat)])] [juxt (map #(% first))])]
(fn [rf] (fn [rf]
((reduce (apply f (sequence args xforms-map))) rf)))) ((reduce (apply f (sequence args xforms-map))) rf))))
([xforms-map coll] ([xforms-map coll]
(transduce (transjuxt xforms-map) first 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)]))))))