Compare commits
9 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78076f8cd0 | ||
|
|
18de93c8fe | ||
|
|
7ae8bed997 | ||
|
|
2079b74271 | ||
|
|
3257561937 | ||
|
|
c6f4b7d041 | ||
|
|
1e23966d5e | ||
|
|
66f3c1ac59 | ||
|
|
a814a220d0 |
8 changed files with 311 additions and 192 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -8,6 +8,9 @@ pom.xml.asc
|
|||
*.class
|
||||
/lib/
|
||||
/classes/
|
||||
/bin/
|
||||
/node_modules/
|
||||
/.lumo_cache/
|
||||
/target/
|
||||
/checkouts/
|
||||
.lein-deps-sum
|
||||
|
|
|
|||
10
README.md
10
README.md
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
More transducers and reducing functions for Clojure(script)!
|
||||
|
||||
[](https://travis-ci.org/cgrand/xforms)
|
||||
|
||||
*Transducers* can be classified in three groups: regular ones, higher-order ones
|
||||
(which accept other transducers as arguments) and aggregators (transducers which emit only 1 item out no matter how many went in).
|
||||
Aggregators generally only make sense in the context of a higher-order transducer.
|
||||
|
|
@ -230,6 +228,14 @@ Evaluation count : 24 in 6 samples of 4 calls.
|
|||
|
||||
## Changelog
|
||||
|
||||
### 0.19.6
|
||||
|
||||
* Fix regression in 0.19.5 #54
|
||||
|
||||
### 0.19.5
|
||||
|
||||
* Support ClojureDart
|
||||
|
||||
### 0.19.4
|
||||
|
||||
* Fix ClojureScript compilation broken in `0.19.3` #49
|
||||
|
|
|
|||
46
build.clj
Normal file
46
build.clj
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
(ns build
|
||||
(:require [clojure.tools.build.api :as b]
|
||||
[clojure.java.shell :as sh]))
|
||||
|
||||
(def lib 'net.cgrand/xforms)
|
||||
(def version "0.19.6" #_(format "0.0.%s" (b/git-count-revs nil)))
|
||||
(def class-dir "target/classes")
|
||||
(def basis (b/create-basis {:project "deps.edn"}))
|
||||
(def jar-file (format "target/%s-%s.jar" (name lib) version))
|
||||
(def scm {:connection "scm:git:git://github.com/cgrand/xforms.git"
|
||||
:developerConnection "scm:git:git://github.com/cgrand/xforms.git"
|
||||
:url "https://github.com/cgrand/xforms"})
|
||||
(def extra-pom-data
|
||||
[[:licenses
|
||||
[:license
|
||||
[:name "Eclipse Public License 1.0"]
|
||||
[:url "https://opensource.org/license/epl-1-0/"]
|
||||
[:distribution "repo"]]
|
||||
[:license
|
||||
[:name "Eclipse Public License 2.0"]
|
||||
[:url "https://opensource.org/license/epl-2-0/"]
|
||||
[:distribution "repo"]]]])
|
||||
|
||||
(defn clean [_]
|
||||
(b/delete {:path "target"}))
|
||||
|
||||
(defn jar [_]
|
||||
(b/write-pom {:class-dir class-dir
|
||||
:lib lib
|
||||
:version version
|
||||
:basis basis
|
||||
:src-dirs ["src"]
|
||||
:scm (assoc scm :tag (str "v" version))
|
||||
:pom-data extra-pom-data})
|
||||
(b/copy-dir {:src-dirs ["src" "resources"]
|
||||
:target-dir class-dir})
|
||||
(b/jar {:class-dir class-dir
|
||||
:jar-file jar-file}))
|
||||
|
||||
(defn clojars [_]
|
||||
(sh/sh
|
||||
"mvn" "deploy:deploy-file" (str "-Dfile=" jar-file)
|
||||
;target/classes/META-INF/maven/net.cgrand/xforms/pom.xml
|
||||
(format "-DpomFile=%s/META-INF/maven/%s/%s/pom.xml"
|
||||
class-dir (namespace lib) (name lib))
|
||||
"-DrepositoryId=clojars" "-Durl=https://clojars.org/repo/"))
|
||||
21
deps.edn
21
deps.edn
|
|
@ -1,10 +1,16 @@
|
|||
{:deps {net.cgrand/macrovich {:mvn/version "0.2.1"}}
|
||||
{:deps {net.cgrand/macrovich {:mvn/version "0.2.2"}}
|
||||
:paths ["src"]
|
||||
|
||||
:aliases
|
||||
{:dev
|
||||
{:extra-paths ["dev"]}
|
||||
|
||||
:cljd
|
||||
{:extra-deps
|
||||
{tensegritics/clojuredart
|
||||
{:git/url "https://github.com/tensegritics/ClojureDart.git"
|
||||
:sha "ae1b485e84ccc35b122f776dfc7cc62198274701"}}}
|
||||
|
||||
:clj-1-9
|
||||
{:extra-deps
|
||||
{org.clojure/clojure {:mvn/version "1.9.0"}
|
||||
|
|
@ -24,9 +30,16 @@
|
|||
{:extra-paths ["test"]}
|
||||
|
||||
:kaocha
|
||||
{:extra-deps {lambdaisland/kaocha {:mvn/version "1.69.1069"}}
|
||||
{:extra-paths ["test"]
|
||||
:extra-deps {lambdaisland/kaocha {:mvn/version "1.69.1069"}}
|
||||
:main-opts ["-m" "kaocha.runner"]}
|
||||
|
||||
:cljs-test-runner
|
||||
{:extra-deps {olical/cljs-test-runner {:mvn/version "3.8.0"}}
|
||||
:main-opts ["-m" "cljs-test-runner.main"]}}}
|
||||
{:extra-paths ["test"]
|
||||
:extra-deps {olical/cljs-test-runner {:mvn/version "3.8.0"}}
|
||||
:main-opts ["-m" "cljs-test-runner.main"]}
|
||||
|
||||
:build
|
||||
{:paths ["."]
|
||||
:deps {io.github.clojure/tools.build {:git/tag "v0.9.6" :git/sha "8e78bcc"}}
|
||||
:ns-default build}}}
|
||||
|
|
|
|||
14
project.clj
14
project.clj
|
|
@ -1,14 +0,0 @@
|
|||
(defproject net.cgrand/xforms "0.19.4"
|
||||
:description "Extra transducers for Clojure"
|
||||
:url "https://github.com/cgrand/xforms"
|
||||
:license {:name "Eclipse Public License"
|
||||
:url "http://www.eclipse.org/legal/epl-v10.html"}
|
||||
|
||||
:plugins [[lein-tools-deps "0.4.5"]]
|
||||
:middleware [lein-tools-deps.plugin/resolve-dependencies-with-deps-edn]
|
||||
:lein-tools-deps/config {:config-files [:project]}
|
||||
|
||||
:profiles
|
||||
{:provided
|
||||
{:dependencies [[org.clojure/clojure "1.8.0"]
|
||||
[org.clojure/clojurescript "1.9.293"]]}})
|
||||
|
|
@ -4,24 +4,31 @@
|
|||
#?(:cljs (:require-macros
|
||||
[net.cgrand.macrovich :as macros]
|
||||
[net.cgrand.xforms :refer [for kvrf let-complete]])
|
||||
:clj (:require [net.cgrand.macrovich :as macros]))
|
||||
:default (:require [net.cgrand.macrovich :as macros]))
|
||||
(:refer-clojure :exclude [some reduce reductions into count for partition
|
||||
str last keys vals min max drop-last take-last
|
||||
sort sort-by time #?@(:bb [] :clj [satisfies?])])
|
||||
(:require [#?(:clj clojure.core :cljs cljs.core) :as core]
|
||||
sort sort-by time #?@(:bb [] :cljd/clj-host [] :clj [satisfies?])])
|
||||
(:require [#?(:cljd cljd.core :clj clojure.core :cljs cljs.core) :as core]
|
||||
[net.cgrand.xforms.rfs :as rf]
|
||||
#?(:clj [clojure.core.protocols]))
|
||||
#?@(:cljd [["dart:collection" :as dart:coll]] :clj [[clojure.core.protocols]] :cljs []))
|
||||
#?(:cljd/clj-host
|
||||
; customize the clj/jvm ns used for macroexpansion
|
||||
(:host-ns (:require [clojure.core :as core]
|
||||
[net.cgrand.macrovich :as macros])))
|
||||
#?(:cljs (:import [goog.structs Queue])))
|
||||
|
||||
(defn- pair? [x] (and (vector? x) (= 2 (core/count x))))
|
||||
(let [kw-or-& #(or (keyword? %) (= '& %))]
|
||||
(defn destructuring-pair? [x]
|
||||
(defn- ^:macro-support pair? [x] (and (vector? x) (= 2 (core/count x))))
|
||||
(def ^:macro-support destructuring-pair?
|
||||
(let [kw-or-& #(or (keyword? %) (= '& %))]
|
||||
(fn [x]
|
||||
(and (pair? x)
|
||||
(not (kw-or-& (first x))))))
|
||||
(not (kw-or-& (first x)))))))
|
||||
|
||||
|
||||
|
||||
(macros/deftime
|
||||
|
||||
(defn- no-user-meta? [x]
|
||||
(defn- ^:macro-support no-user-meta? [x]
|
||||
(= {} (dissoc (or (meta x) {}) :file :line :column :end-line :end-column)))
|
||||
|
||||
(defmacro unreduced->
|
||||
|
|
@ -82,7 +89,7 @@
|
|||
([~acc] (~rf ~acc))
|
||||
([~acc ~binding] ~body)))))))
|
||||
|
||||
(defn- arity [[arglist & body :as fn-body]]
|
||||
(defn- ^:macro-support arity [[arglist & body :as fn-body]]
|
||||
(let [[fixargs varargs] (split-with (complement #{'&}) arglist)]
|
||||
(if (seq varargs) (zipmap (range (core/count fixargs) 4) (repeat fn-body)))
|
||||
{(core/count fixargs) fn-body}))
|
||||
|
|
@ -98,20 +105,27 @@
|
|||
(if (destructuring-pair? arg)
|
||||
(let [[karg varg] arg]
|
||||
`([~acc ~karg ~varg] ~@body))
|
||||
`([~acc k# v#] (let [~arg (macros/case :clj (clojure.lang.MapEntry. k# v#) :cljs [k# v#])] ~@body)))))
|
||||
(let [k (gensym "k__")
|
||||
v (gensym "v__")
|
||||
arg-value (macros/case
|
||||
:clj `(clojure.lang.MapEntry. ~k ~v)
|
||||
:cljs [k v]
|
||||
:cljd `(MapEntry ~k ~v))]
|
||||
`([~acc ~k ~v] (let [~arg ~arg-value] ~@body))))))
|
||||
(not (arities 2)) (conj (let [[[acc karg varg] & body] (arities 3)]
|
||||
`([~acc [~karg ~varg]] ~@body))))]
|
||||
`(reify
|
||||
#?@(:bb [] ;; babashka currently only supports reify with one Java interface at a time
|
||||
:default [~@(macros/case :clj '[clojure.lang.Fn])])
|
||||
:default [~@(macros/case :cljd '[cljd.core/Fn] :clj '[clojure.lang.Fn])])
|
||||
KvRfable
|
||||
(some-kvrf [this#] this#)
|
||||
~(macros/case :cljs `core/IFn :clj 'clojure.lang.IFn)
|
||||
(~'some-kvrf [this#] this#)
|
||||
~(macros/case :cljs `core/IFn :clj 'clojure.lang.IFn :cljd 'cljd.core/IFn)
|
||||
~@(core/for [[args & body] fn-bodies]
|
||||
(let [nohint-args (map (fn [arg] (if (:tag (meta arg)) (gensym 'arg) arg)) args)
|
||||
rebind (mapcat (fn [arg nohint]
|
||||
(when-not (= arg nohint) [arg nohint])) args nohint-args)]
|
||||
`(~(macros/case :cljs `core/-invoke :clj 'invoke) [~name ~@nohint-args] ~@(if (seq rebind) [`(let [~@rebind] ~@body)] body)))))))
|
||||
`(~(macros/case :cljd '-invoke :cljs `core/-invoke :clj 'invoke)
|
||||
[~name ~@nohint-args] ~@(if (seq rebind) [`(let [~@rebind] ~@body)] body)))))))
|
||||
|
||||
(defmacro ^:private let-complete [[binding volatile] & body]
|
||||
`(let [v# @~volatile]
|
||||
|
|
@ -131,6 +145,7 @@
|
|||
;; Workaround clojure.core/satisfies? being slow in Clojure
|
||||
;; see https://ask.clojure.org/index.php/3304/make-satisfies-as-fast-as-a-protocol-method-call
|
||||
#?(:bb nil
|
||||
:cljd nil
|
||||
:clj
|
||||
(defn fast-satisfies?-fn
|
||||
"Ported from https://github.com/clj-commons/manifold/blob/37658e91f836047a630586a909a2e22debfbbfc6/src/manifold/utils.clj#L77-L89"
|
||||
|
|
@ -147,11 +162,14 @@
|
|||
val)
|
||||
val))))))
|
||||
|
||||
|
||||
#?(:cljs
|
||||
(defn kvreducible? [coll]
|
||||
(satisfies? IKVReduce coll))
|
||||
|
||||
:cljd
|
||||
(defn kvreducible? [coll]
|
||||
(satisfies? cljd.core/IKVReduce coll))
|
||||
|
||||
:clj
|
||||
(let [satisfies-ikvreduce? #?(:bb #(satisfies? clojure.core.protocols/IKVReduce %)
|
||||
:default (fast-satisfies?-fn #'clojure.core.protocols/IKVReduce))]
|
||||
|
|
@ -163,7 +181,7 @@
|
|||
|
||||
|
||||
(extend-protocol KvRfable
|
||||
#?(:clj Object :cljs default) (some-kvrf [_] nil)
|
||||
#?(:cljd fallback :clj Object :cljs default) (some-kvrf [_] nil)
|
||||
#?@(:clj [nil (some-kvrf [_] nil)]))
|
||||
|
||||
(defn ensure-kvrf [rf]
|
||||
|
|
@ -197,7 +215,8 @@
|
|||
|
||||
(defn- into-rf [to]
|
||||
(cond
|
||||
#?(:clj (instance? clojure.lang.IEditableCollection to)
|
||||
#?(:cljd (satisfies? cljd.core/IEditableCollection to)
|
||||
:clj (instance? clojure.lang.IEditableCollection to)
|
||||
:cljs (satisfies? IEditableCollection to))
|
||||
(if (map? to)
|
||||
(kvrf
|
||||
|
|
@ -235,7 +254,8 @@
|
|||
|
||||
(defn- without-rf [from]
|
||||
(cond
|
||||
#?(:clj (instance? clojure.lang.IEditableCollection from)
|
||||
#?(:cljd (satisfies? cljd.core/IEditableCollection from)
|
||||
:clj (instance? clojure.lang.IEditableCollection from)
|
||||
:cljs (satisfies? IEditableCollection from))
|
||||
(if (map? from)
|
||||
(fn
|
||||
|
|
@ -410,7 +430,11 @@
|
|||
(comp (apply by-key by-key-args) (into coll)))
|
||||
|
||||
(macros/replace
|
||||
[#?(:cljs {(java.util.ArrayDeque. n) (Queue.)
|
||||
[#?(:cljd {(java.util.ArrayDeque. n) (dart:coll/Queue)
|
||||
.add .add
|
||||
.poll .removeFirst
|
||||
.size .-length})
|
||||
#?(:cljs {(java.util.ArrayDeque. n) (Queue.)
|
||||
.add .enqueue
|
||||
.poll .dequeue
|
||||
.size .getCount})
|
||||
|
|
@ -424,7 +448,7 @@
|
|||
(if (fn? step-or-xform)
|
||||
(partition n n step-or-xform)
|
||||
(partition n step-or-xform (into []))))
|
||||
([^long n step pad-or-xform]
|
||||
([#?(:cljd ^int n :default ^long n) step pad-or-xform]
|
||||
(if (fn? pad-or-xform)
|
||||
(let [xform pad-or-xform]
|
||||
(fn [rf]
|
||||
|
|
@ -446,7 +470,7 @@
|
|||
acc)
|
||||
acc)))))))
|
||||
(partition n step pad-or-xform (into []))))
|
||||
([^long n step pad xform]
|
||||
([#?(:cljd ^int n :default ^long n) step pad xform]
|
||||
(fn [rf]
|
||||
(let [mxrf (multiplexable rf)
|
||||
dq (java.util.ArrayDeque. n)
|
||||
|
|
@ -477,7 +501,7 @@
|
|||
(fn [rf]
|
||||
(let )))
|
||||
|
||||
(defn take-last [^long n]
|
||||
(defn take-last [#?(:cljd ^int n :default ^long n)]
|
||||
(fn [rf]
|
||||
(let [dq (java.util.ArrayDeque. n)]
|
||||
(fn
|
||||
|
|
@ -490,7 +514,7 @@
|
|||
|
||||
(defn drop-last
|
||||
([] (drop-last 1))
|
||||
([^long n]
|
||||
([#?(:cljd ^int n :default ^long n)]
|
||||
(fn [rf]
|
||||
(let [dq (java.util.ArrayDeque. n)
|
||||
xform (map #(if (identical? dq %) nil %))
|
||||
|
|
@ -527,17 +551,20 @@
|
|||
([] (sort compare))
|
||||
([cmp]
|
||||
(fn [rf]
|
||||
(let [buf #?(:clj (java.util.ArrayList.) :cljs #js [])]
|
||||
(let [buf #?(:cljd #dart [] :clj (java.util.ArrayList.) :cljs #js [])]
|
||||
(fn
|
||||
([] (rf))
|
||||
([acc] (rf (core/reduce rf acc (doto buf #?(:clj (java.util.Collections/sort cmp) :cljs (.sort (fn->comparator cmp)))))))
|
||||
([acc x] (#?(:clj .add :cljs .push) buf x) acc))))))
|
||||
([acc] (rf (core/reduce rf acc (doto buf #?(:cljd (.sort (dart-comparator cmp))
|
||||
:clj (java.util.Collections/sort cmp)
|
||||
:cljs (.sort (fn->comparator cmp)))))))
|
||||
([acc x] (#?(:cljd .add :clj .add :cljs .push) buf x) acc))))))
|
||||
|
||||
(defn sort-by
|
||||
([kfn] (sort-by kfn compare))
|
||||
([kfn cmp]
|
||||
(sort (fn [a b]
|
||||
#?(:clj (.compare ^java.util.Comparator cmp (kfn a) (kfn b))
|
||||
#?(:cljd (cmp (kfn a) (kfn b))
|
||||
:clj (.compare ^java.util.Comparator cmp (kfn a) (kfn b))
|
||||
:cljs (cmp (kfn a) (kfn b)))))))
|
||||
|
||||
(defn reductions
|
||||
|
|
@ -603,7 +630,8 @@
|
|||
(vreset! vi (let [i (inc i)] (if (= n i) 0 i)))
|
||||
(rf acc (f (vreset! vwacc (f (invf wacc x') x))))))))))))
|
||||
|
||||
#?(:clj
|
||||
#?(:cljd nil
|
||||
:clj
|
||||
(defn iterator
|
||||
"Iterator transducing context, returns an iterator on the transformed data.
|
||||
Equivalent to (.iterator (eduction xform (iterator-seq src-iterator))) except there's is no buffering on values (as in iterator-seq),
|
||||
|
|
@ -634,7 +662,8 @@
|
|||
(if (identical? NULL x) nil x))
|
||||
(throw (java.util.NoSuchElementException.))))))))
|
||||
|
||||
#?(:clj
|
||||
#?(:cljd nil
|
||||
:clj
|
||||
(defn window-by-time
|
||||
"ALPHA
|
||||
Returns a transducer which computes a windowed accumulator over chronologically sorted items.
|
||||
|
|
@ -700,11 +729,11 @@
|
|||
"Count the number of items. Either used directly as a transducer or invoked with two args
|
||||
as a transducing context."
|
||||
([rf]
|
||||
(let [n #?(:clj (java.util.concurrent.atomic.AtomicLong.) :cljs (atom 0))]
|
||||
(let [n #?(:cljd (volatile! 0) :clj (java.util.concurrent.atomic.AtomicLong.) :cljs (volatile! 0))]
|
||||
(fn
|
||||
([] (rf))
|
||||
([acc] (rf (unreduced (rf acc #?(:clj (.get n) :cljs @n)))))
|
||||
([acc _] #?(:clj (.incrementAndGet n) :cljs (swap! n inc)) acc))))
|
||||
([acc] (rf (unreduced (rf acc #?(:cljd @n :clj (.get n) :cljs @n)))))
|
||||
([acc _] #?(:cljd (vswap! n inc) :clj (.incrementAndGet n) :cljs (vswap! n inc)) acc))))
|
||||
([xform coll]
|
||||
(transduce (comp xform count) rf/last coll)))
|
||||
|
||||
|
|
@ -798,6 +827,8 @@
|
|||
(.addAndGet at (- (System/nanoTime) t)) (swap! at + (- (system-time) t))
|
||||
.size .getCount})]
|
||||
|
||||
#?(:cljd nil
|
||||
:default
|
||||
(defn time
|
||||
"Measures the time spent in this transformation and prints the measured time.
|
||||
tag-or-f may be either a function of 1 argument (measured time in ms) in which case
|
||||
|
|
@ -834,7 +865,7 @@
|
|||
(let [t (System/nanoTime)
|
||||
r (rf acc x)]
|
||||
(.addAndGet at (- (System/nanoTime) t))
|
||||
r)))))))))
|
||||
r))))))))))
|
||||
|
||||
#_(defn rollup
|
||||
"Roll-up input data along the provided dimensions (which are functions of one input item),
|
||||
|
|
|
|||
|
|
@ -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!)
|
||||
|
||||
|
|
@ -28,40 +31,59 @@
|
|||
:else 0))))
|
||||
|
||||
(defn minimum
|
||||
([#?(:clj ^java.util.Comparator comparator :cljs comparator)]
|
||||
([#?(: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? (#?(:clj .compare :cljs cmp) comparator a b)) b
|
||||
:else a))))
|
||||
([#?(:clj ^java.util.Comparator comparator :cljs comparator) absolute-maximum]
|
||||
(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? (#?(:clj .compare :cljs cmp) comparator a b))) b a)))))
|
||||
([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)]
|
||||
([#?(: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? (#?(:clj .compare :cljs cmp) comparator a b)) b
|
||||
:else a))))
|
||||
([#?(:clj ^java.util.Comparator comparator :cljs comparator) absolute-minimum]
|
||||
(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? (#?(:clj .compare :cljs cmp) comparator a b))) b a)))))
|
||||
([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] ; weighted mean
|
||||
(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,14 @@
|
|||
|
||||
(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] (doto (or-instance?
|
||||
#?(:cljd StringBuffer :clj StringBuilder :cljs StringBuffer) sb
|
||||
(#?(:cljd StringBuffer :clj StringBuilder. :cljs StringBuffer.) (core/str sb)))
|
||||
(#?(:cljd .write :default .append) x))))
|
||||
|
||||
(def str
|
||||
"Reducing function to build strings in linear time. Acts as replacement for clojure.core/str in a reduce/transduce call."
|
||||
|
|
|
|||
|
|
@ -158,3 +158,9 @@
|
|||
'(a b) false
|
||||
'{foo bar} false
|
||||
'{foo :bar} false)))
|
||||
|
||||
(defmacro wraps-for-with-no-destructuring []
|
||||
(x/into [] (x/for [x (range 5)] x)))
|
||||
|
||||
(deftest for-in-macro
|
||||
(is (= [0 1 2 3 4] (wraps-for-with-no-destructuring))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue