From cffd76298bdfe9d7048ab80ed21be771090e51b8 Mon Sep 17 00:00:00 2001 From: Christophe Grand Date: Fri, 16 Dec 2016 14:52:59 +0100 Subject: [PATCH] near total port (window-time is missing) --- src/net/cgrand/xforms.cljc | 23 ++++++++++++----------- src/net/cgrand/xforms/rfs.cljc | 21 +++++++++++++++------ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/net/cgrand/xforms.cljc b/src/net/cgrand/xforms.cljc index 754721b..57117a6 100644 --- a/src/net/cgrand/xforms.cljc +++ b/src/net/cgrand/xforms.cljc @@ -7,7 +7,8 @@ :clj (:require [net.cgrand.macrovich :as macros])) (:refer-clojure :exclude [reduce reductions into count for partition str last keys vals min max]) (:require [#?(:clj clojure.core :cljs cljs.core) :as core] - [net.cgrand.xforms.rfs :as rf])) + [net.cgrand.xforms.rfs :as rf]) + #?(:cljs (:import [goog.structs Queue]))) (macros/deftime @@ -169,7 +170,7 @@ (defn maximum ([comparator] (maximum comparator nil)) - ([^java.util.Comparator comparator absolute-minimum] + ([comparator absolute-minimum] (reduce (rf/maximum comparator absolute-minimum)))) (def min (reduce rf/min)) @@ -284,7 +285,7 @@ (let [xform pad-or-xform] (fn [rf] (let [mxrf (multiplexable rf) - dq (java.util.ArrayDeque. n) + dq #?(:clj (java.util.ArrayDeque. n) :cljs (Queue.)) barrier (volatile! n) xform (comp (map #(if (identical? dq %) nil %)) xform)] (fn @@ -292,11 +293,11 @@ ([acc] (.clear dq) (rf acc)) ([acc x] (let [b (vswap! barrier dec)] - (when (< b n) (.add dq (if (nil? x) dq x))) + (when (< b n) (#?(:clj .add :cljs .enqueue) dq (if (nil? x) dq x))) (if (zero? b) ; this transduce may return a reduced because of mxrf wrapping reduceds coming from rf - (let [acc (transduce xform mxrf acc dq)] - (dotimes [_ (core/min n step)] (.poll dq)) + (let [acc (transduce xform mxrf acc #?(:clj dq :cljs (.getValues dq)))] + (dotimes [_ (core/min n step)] (#?(:clj .poll :cljs .dequeue) dq)) (vswap! barrier + step) acc) acc))))))) @@ -304,7 +305,7 @@ ([n step pad xform] (fn [rf] (let [mxrf (multiplexable rf) - dq (java.util.ArrayDeque. n) + dq #?(:clj (java.util.ArrayDeque. n) :cljs (Queue.)) barrier (volatile! n) xform (comp (map #(if (identical? dq %) nil %)) xform)] (fn @@ -312,18 +313,18 @@ ([acc] (if (< @barrier n) (let [xform (comp cat (take n) xform) ; don't use mxrf for completion: we want completion and don't want reduced-wrapping - acc (transduce xform rf acc [dq pad])] + acc (transduce xform rf acc [(.getValues dq) pad])] (vreset! @barrier n) (.clear dq) acc) acc)) ([acc x] (let [b (vswap! barrier dec)] - (when (< b n) (.add dq (if (nil? x) dq x))) + (when (< b n) (#?(:clj .add :cljs .enqueue) dq (if (nil? x) dq x))) (if (zero? b) ; this transduce may return a reduced because of mxrf wrapping reduceds coming from rf - (let [acc (transduce xform mxrf acc dq)] - (dotimes [_ (min n step)] (.poll dq)) + (let [acc (transduce xform mxrf acc #?(:clj dq :cljs (.getValues dq)))] + (dotimes [_ (min n step)] (#?(:clj .poll :cljs .dequeue) dq)) (vswap! barrier + step) acc) acc)))))))) diff --git a/src/net/cgrand/xforms/rfs.cljc b/src/net/cgrand/xforms/rfs.cljc index 4d43b4f..4d9f80d 100644 --- a/src/net/cgrand/xforms/rfs.cljc +++ b/src/net/cgrand/xforms/rfs.cljc @@ -17,28 +17,37 @@ (declare str!) (macros/usetime + +#? (:cljs + (defn ^:private cmp [f a b] + (let [r (f a b)] + (cond + (number? r) r + r -1 + (f b a) 1 + :else 0)))) (defn minimum ([comparator] (minimum comparator nil)) - ([^java.util.Comparator comparator absolute-maximum] + ([#?(:clj ^java.util.Comparator comparator :cljs comparator) absolute-maximum] (fn ([] ::+∞) - ([x] (if (identical? ::+∞ x) + ([x] (if (#?(:clj identical? :cljs keyword-identical?) ::+∞ x) absolute-maximum x)) - ([a b] (if (or (identical? ::+∞ a) (pos? (.compare comparator a b))) b a))))) + ([a b] (if (or (#?(:clj identical? :cljs keyword-identical?) ::+∞ a) (pos? (#?(:clj .compare :cljs cmp) comparator a b))) b a))))) (defn maximum ([comparator] (maximum comparator nil)) - ([^java.util.Comparator comparator absolute-minimum] + ([#?(:clj ^java.util.Comparator comparator :cljs comparator) absolute-minimum] (fn ([] ::-∞) - ([x] (if (identical? ::-∞ x) + ([x] (if (#?(:clj identical? :cljs keyword-identical?) ::-∞ x) absolute-minimum x)) - ([a b] (if (or (identical? ::-∞ a) (neg? (.compare comparator a b))) b a))))) + ([a b] (if (or (#?(:clj identical? :cljs keyword-identical?) ::-∞ a) (neg? (#?(:clj .compare :cljs cmp) comparator a b))) b a))))) (def min (minimum compare))