rfs done for cljd
This commit is contained in:
parent
550dbc150a
commit
eeecceb85e
1 changed files with 74 additions and 45 deletions
|
|
@ -6,13 +6,16 @@
|
|||
[net.cgrand.xforms.rfs :refer [or-instance?]])
|
||||
:clj (:require [net.cgrand.macrovich :as macros]))
|
||||
(:require [#?(:clj clojure.core :cljs cljs.core) :as core])
|
||||
#?(:cljd (:require ["dart:math" :as Math]))
|
||||
#?(:cljs (:import [goog.string StringBuffer])))
|
||||
|
||||
(macros/deftime
|
||||
(defmacro ^:private or-instance? [class x y]
|
||||
(let [xsym (gensym 'x_)]
|
||||
`(let [~xsym ~x]
|
||||
(if (instance? ~class ~xsym) ~(with-meta xsym {:tag class}) ~y)))))
|
||||
(if #?(:cljd (dart/is? ~xsym ~class)
|
||||
:default (instance? ~class ~xsym))
|
||||
~(with-meta xsym {:tag class}) ~y)))))
|
||||
|
||||
(declare str!)
|
||||
|
||||
|
|
@ -26,42 +29,61 @@
|
|||
r -1
|
||||
(f b a) 1
|
||||
:else 0))))
|
||||
|
||||
|
||||
(defn minimum
|
||||
([#?(:clj ^java.util.Comparator comparator :cljs comparator)]
|
||||
(fn
|
||||
([] nil)
|
||||
([x] x)
|
||||
([a b] (cond
|
||||
(nil? a) b
|
||||
(nil? b) a
|
||||
(pos? (#?(:clj .compare :cljs cmp) comparator a b)) b
|
||||
:else a))))
|
||||
([#?(:clj ^java.util.Comparator comparator :cljs comparator) absolute-maximum]
|
||||
(fn
|
||||
([] ::+infinity)
|
||||
([x] (if (#?(:clj identical? :cljs keyword-identical?) ::+infinity x)
|
||||
absolute-maximum
|
||||
x))
|
||||
([a b] (if (or (#?(:clj identical? :cljs keyword-identical?) ::+infinity a) (pos? (#?(:clj .compare :cljs cmp) comparator a b))) b a)))))
|
||||
([#?(:cljd comparator :clj ^java.util.Comparator comparator :cljs comparator)]
|
||||
(let [#?@(:cljd [comparator (dart-comparator comparator)] :default [])]
|
||||
(fn
|
||||
([] nil)
|
||||
([x] x)
|
||||
([a b] (cond
|
||||
(nil? a) b
|
||||
(nil? b) a
|
||||
(pos? #?(:cljd (comparator a b)
|
||||
:clj (.compare comparator a b)
|
||||
:cljs (cmp comparator a b))) b
|
||||
:else a)))))
|
||||
([#?(:cljd comparator :clj ^java.util.Comparator comparator :cljs comparator) absolute-maximum]
|
||||
(let [#?@(:cljd [comparator (dart-comparator comparator)] :default [])]
|
||||
(fn
|
||||
([] ::+infinity)
|
||||
([x] (if (#?(:clj identical? :cljs keyword-identical?) ::+infinity x)
|
||||
absolute-maximum
|
||||
x))
|
||||
([a b]
|
||||
(if (or
|
||||
(#?(:clj identical? :cljs keyword-identical?) ::+infinity a)
|
||||
(pos? #?(:cljd (comparator a b)
|
||||
:clj (.compare comparator a b)
|
||||
:cljs (cmp comparator a b))))
|
||||
b a))))))
|
||||
|
||||
(defn maximum
|
||||
([#?(:clj ^java.util.Comparator comparator :cljs comparator)]
|
||||
(fn
|
||||
([] nil)
|
||||
([x] x)
|
||||
([a b] (cond
|
||||
(nil? a) b
|
||||
(nil? b) a
|
||||
(neg? (#?(:clj .compare :cljs cmp) comparator a b)) b
|
||||
:else a))))
|
||||
([#?(:clj ^java.util.Comparator comparator :cljs comparator) absolute-minimum]
|
||||
(fn
|
||||
([] ::-infinity)
|
||||
([x] (if (#?(:clj identical? :cljs keyword-identical?) ::-infinity x)
|
||||
absolute-minimum
|
||||
x))
|
||||
([a b] (if (or (#?(:clj identical? :cljs keyword-identical?) ::-infinity a) (neg? (#?(:clj .compare :cljs cmp) comparator a b))) b a)))))
|
||||
([#?(:cljd comparator :clj ^java.util.Comparator comparator :cljs comparator)]
|
||||
(let [#?@(:cljd [comparator (dart-comparator comparator)] :default [])]
|
||||
(fn
|
||||
([] nil)
|
||||
([x] x)
|
||||
([a b] (cond
|
||||
(nil? a) b
|
||||
(nil? b) a
|
||||
(neg? #?(:cljd (comparator a b)
|
||||
:clj (.compare comparator a b)
|
||||
:cljs (cmp comparator a b))) b
|
||||
:else a)))))
|
||||
([#?(:cljd comparator :clj ^java.util.Comparator comparator :cljs comparator) absolute-minimum]
|
||||
(let [#?@(:cljd [comparator (dart-comparator comparator)] :default [])]
|
||||
(fn
|
||||
([] ::-infinity)
|
||||
([x] (if (#?(:clj identical? :cljs keyword-identical?) ::-infinity x)
|
||||
absolute-minimum
|
||||
x))
|
||||
([a b]
|
||||
(if (or (#?(:clj identical? :cljs keyword-identical?) ::-infinity a)
|
||||
(neg? #?(:cljd (comparator a b)
|
||||
:clj (.compare comparator a b)
|
||||
:cljs (cmp comparator a b))))
|
||||
b a))))))
|
||||
|
||||
(def min (minimum compare))
|
||||
|
||||
|
|
@ -70,23 +92,24 @@
|
|||
(defn avg
|
||||
"Reducing fn to compute the arithmetic mean."
|
||||
([] nil)
|
||||
([^doubles acc] (when acc (/ (aget acc 1) (aget acc 0))))
|
||||
([#?(:cljd ^{:tag #/(List double)} acc :clj ^doubles acc :cljs ^doubles acc)]
|
||||
(when acc (/ (aget acc 1) (aget acc 0))))
|
||||
([acc x] (avg acc x 1))
|
||||
([^doubles acc x w] ; weighted mean
|
||||
(let [acc (or acc #?(:clj (double-array 3) :cljs #js [0.0 0.0]))]
|
||||
([#?(:cljd ^{:tag #/(List double)} acc :clj ^doubles acc :cljs ^doubles acc) x w]
|
||||
(let [acc (or acc #?(:cljd (double-array 2) :clj (double-array 2) :cljs #js [0.0 0.0]))]
|
||||
(doto acc
|
||||
(aset 0 (+ (aget acc 0) w))
|
||||
(aset 1 (+ (aget acc 1) (* w x)))))))
|
||||
|
||||
(defn sd
|
||||
"Reducing fn to compute the standard deviation. Returns 0 if no or only one item."
|
||||
([] #?(:clj (double-array 3) :cljs #js [0.0 0.0 0.0]))
|
||||
([^doubles a]
|
||||
([] #?(:cljd (double-array 3) :clj (double-array 3) :cljs #js [0.0 0.0 0.0]))
|
||||
([#?(:cljd ^{:tag #/(List double)} a :default ^doubles a)]
|
||||
(let [s (aget a 0) n (aget a 2)]
|
||||
(if (< 1 n)
|
||||
(Math/sqrt (/ s (dec n)))
|
||||
0.0)))
|
||||
([^doubles a x]
|
||||
([#?(:cljd ^{:tag #/(List double)} a :default ^doubles a) x]
|
||||
(let [s (aget a 0) m (aget a 1) n (aget a 2)
|
||||
d (- x m)
|
||||
n (inc n)
|
||||
|
|
@ -110,9 +133,15 @@
|
|||
|
||||
(defn str!
|
||||
"Like xforms/str but returns a StringBuilder."
|
||||
([] (#?(:clj StringBuilder. :cljs StringBuffer.)))
|
||||
([sb] (or-instance? #?(:clj StringBuilder :cljs StringBuffer) sb (#?(:clj StringBuilder. :cljs StringBuffer.) (core/str sb)))) ; the instance? checks are for compatibility with str in case of seeded reduce/transduce.
|
||||
([sb x] (.append (or-instance? #?(:clj StringBuilder :cljs StringBuffer) sb (#?(:clj StringBuilder. :cljs StringBuffer.) (core/str sb))) x)))
|
||||
([] (#?(:cljd StringBuffer :clj StringBuilder. :cljs StringBuffer.)))
|
||||
([sb] (or-instance? #?(:cljd StringBuffer :clj StringBuilder :cljs StringBuffer) sb
|
||||
(#?(:cljd StringBuffer :clj StringBuilder. :cljs StringBuffer.) (core/str sb))))
|
||||
; the instance? checks are for compatibility with str in case of seeded reduce/transduce.
|
||||
([sb x] (#?(:cljd .write :default .append)
|
||||
(or-instance?
|
||||
#?(:cljd StringBuffer :clj StringBuilder :cljs StringBuffer) sb
|
||||
(#?(:cljd StringBuffer :clj StringBuilder. :cljs StringBuffer.) (core/str sb)))
|
||||
x)))
|
||||
|
||||
(def str
|
||||
"Reducing function to build strings in linear time. Acts as replacement for clojure.core/str in a reduce/transduce call."
|
||||
|
|
@ -127,12 +156,12 @@
|
|||
([] (mapv #(vector % (volatile! (%))) rfns))
|
||||
([acc] (mapv (fn [[rf vacc]] (rf (unreduced @vacc))) acc))
|
||||
([acc x]
|
||||
(let [some-unreduced (core/reduce (fn [some-unreduced [rf vacc]]
|
||||
(let [some-unreduced (core/reduce (fn [some-unreduced [rf vacc]]
|
||||
(when-not (reduced? @vacc) (vswap! vacc rf x) true))
|
||||
false acc)]
|
||||
(if some-unreduced acc (reduced acc))))
|
||||
([acc k v]
|
||||
(let [some-unreduced (core/reduce (fn [some-unreduced [rf vacc]]
|
||||
(let [some-unreduced (core/reduce (fn [some-unreduced [rf vacc]]
|
||||
(when-not (reduced? @vacc) (vswap! vacc rf k v) true))
|
||||
false acc)]
|
||||
(if some-unreduced acc (reduced acc)))))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue