use macros/replace
This commit is contained in:
parent
0422678643
commit
d4f0280bb5
2 changed files with 63 additions and 57 deletions
|
|
@ -5,4 +5,4 @@
|
||||||
:url "http://www.eclipse.org/legal/epl-v10.html"}
|
:url "http://www.eclipse.org/legal/epl-v10.html"}
|
||||||
:dependencies [[org.clojure/clojure "1.8.0"]
|
:dependencies [[org.clojure/clojure "1.8.0"]
|
||||||
[org.clojure/clojurescript "1.9.293"]
|
[org.clojure/clojurescript "1.9.293"]
|
||||||
[net.cgrand/macrovich "0.1.0"]])
|
[net.cgrand/macrovich "0.2.0"]])
|
||||||
|
|
|
||||||
|
|
@ -273,62 +273,68 @@
|
||||||
acc)))
|
acc)))
|
||||||
([acc k v] (self acc #?(:clj (clojure.lang.MapEntry. k v) :cljs [k v])))))))))))
|
([acc k v] (self acc #?(:clj (clojure.lang.MapEntry. k v) :cljs [k v])))))))))))
|
||||||
|
|
||||||
(defn partition
|
(macros/replace
|
||||||
"Returns a partitioning transducer. Each partition is independently transformed using the xform transducer."
|
[#?(:cljs {(java.util.ArrayDeque. n) (Queue.)
|
||||||
([n]
|
.add .enqueue
|
||||||
(partition n n (into [])))
|
.poll .dequeue})
|
||||||
([n step-or-xform]
|
#?(:clj {(.getValues dq) dq})]
|
||||||
(if (fn? step-or-xform)
|
|
||||||
(partition n n step-or-xform)
|
(defn partition
|
||||||
(partition n step-or-xform (into []))))
|
"Returns a partitioning transducer. Each partition is independently transformed using the xform transducer."
|
||||||
([n step pad-or-xform]
|
([n]
|
||||||
(if (fn? pad-or-xform)
|
(partition n n (into [])))
|
||||||
(let [xform pad-or-xform]
|
([n step-or-xform]
|
||||||
(fn [rf]
|
(if (fn? step-or-xform)
|
||||||
(let [mxrf (multiplexable rf)
|
(partition n n step-or-xform)
|
||||||
dq #?(:clj (java.util.ArrayDeque. n) :cljs (Queue.))
|
(partition n step-or-xform (into []))))
|
||||||
barrier (volatile! n)
|
([n step pad-or-xform]
|
||||||
xform (comp (map #(if (identical? dq %) nil %)) xform)]
|
(if (fn? pad-or-xform)
|
||||||
(fn
|
(let [xform pad-or-xform]
|
||||||
([] (rf))
|
(fn [rf]
|
||||||
([acc] (.clear dq) (rf acc))
|
(let [mxrf (multiplexable rf)
|
||||||
([acc x]
|
dq (java.util.ArrayDeque. n)
|
||||||
(let [b (vswap! barrier dec)]
|
barrier (volatile! n)
|
||||||
(when (< b n) (#?(:clj .add :cljs .enqueue) dq (if (nil? x) dq x)))
|
xform (comp (map #(if (identical? dq %) nil %)) xform)]
|
||||||
(if (zero? b)
|
(fn
|
||||||
; this transduce may return a reduced because of mxrf wrapping reduceds coming from rf
|
([] (rf))
|
||||||
(let [acc (transduce xform mxrf acc #?(:clj dq :cljs (.getValues dq)))]
|
([acc] (.clear dq) (rf acc))
|
||||||
(dotimes [_ (core/min n step)] (#?(:clj .poll :cljs .dequeue) dq))
|
([acc x]
|
||||||
(vswap! barrier + step)
|
(let [b (vswap! barrier dec)]
|
||||||
acc)
|
(when (< b n) (.add dq (if (nil? x) dq x)))
|
||||||
acc)))))))
|
(if (zero? b)
|
||||||
(partition n step pad-or-xform (into []))))
|
; this transduce may return a reduced because of mxrf wrapping reduceds coming from rf
|
||||||
([n step pad xform]
|
(let [acc (transduce xform mxrf acc (.getValues dq))]
|
||||||
(fn [rf]
|
(dotimes [_ (core/min n step)] (.poll dq))
|
||||||
(let [mxrf (multiplexable rf)
|
(vswap! barrier + step)
|
||||||
dq #?(:clj (java.util.ArrayDeque. n) :cljs (Queue.))
|
acc)
|
||||||
barrier (volatile! n)
|
acc)))))))
|
||||||
xform (comp (map #(if (identical? dq %) nil %)) xform)]
|
(partition n step pad-or-xform (into []))))
|
||||||
(fn
|
([n step pad xform]
|
||||||
([] (rf))
|
(fn [rf]
|
||||||
([acc] (if (< @barrier n)
|
(let [mxrf (multiplexable rf)
|
||||||
(let [xform (comp cat (take n) xform)
|
dq (java.util.ArrayDeque. n)
|
||||||
; don't use mxrf for completion: we want completion and don't want reduced-wrapping
|
barrier (volatile! n)
|
||||||
acc (transduce xform rf acc [(.getValues dq) pad])]
|
xform (comp (map #(if (identical? dq %) nil %)) xform)]
|
||||||
(vreset! @barrier n)
|
(fn
|
||||||
(.clear dq)
|
([] (rf))
|
||||||
acc)
|
([acc] (if (< @barrier n)
|
||||||
acc))
|
(let [xform (comp cat (take n) xform)
|
||||||
([acc x]
|
; don't use mxrf for completion: we want completion and don't want reduced-wrapping
|
||||||
(let [b (vswap! barrier dec)]
|
acc (transduce xform rf acc [(.getValues dq) pad])]
|
||||||
(when (< b n) (#?(:clj .add :cljs .enqueue) dq (if (nil? x) dq x)))
|
(vreset! @barrier n)
|
||||||
(if (zero? b)
|
(.clear dq)
|
||||||
; this transduce may return a reduced because of mxrf wrapping reduceds coming from rf
|
acc)
|
||||||
(let [acc (transduce xform mxrf acc #?(:clj dq :cljs (.getValues dq)))]
|
acc))
|
||||||
(dotimes [_ (min n step)] (#?(:clj .poll :cljs .dequeue) dq))
|
([acc x]
|
||||||
(vswap! barrier + step)
|
(let [b (vswap! barrier dec)]
|
||||||
acc)
|
(when (< b n) (.add dq (if (nil? x) dq x)))
|
||||||
acc))))))))
|
(if (zero? b)
|
||||||
|
; this transduce may return a reduced because of mxrf wrapping reduceds coming from rf
|
||||||
|
(let [acc (transduce xform mxrf acc (.getValues dq))]
|
||||||
|
(dotimes [_ (min n step)] (.poll dq))
|
||||||
|
(vswap! barrier + step)
|
||||||
|
acc)
|
||||||
|
acc)))))))))
|
||||||
|
|
||||||
(defn reductions
|
(defn reductions
|
||||||
"Transducer version of reductions. There's a difference in behavior when init is not provided: (f) is used.
|
"Transducer version of reductions. There's a difference in behavior when init is not provided: (f) is used.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue