-
Notifications
You must be signed in to change notification settings - Fork 6
/
stat.cljc
71 lines (59 loc) · 2.15 KB
/
stat.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(ns axel-f.excel.stat
(:require [axel-f.excel.math :as math]
[axel-f.excel.coerce :as coerce]))
(defn- flatten-numbers [tr-coercer]
(comp (mapcat #(cond
(string? %) [(not-empty %)]
(sequential? %) (not-empty %)
(boolean? %) [%]
(number? %) [%]
:else nil))
(filter identity)
tr-coercer
(filter number?)))
(defn AVERAGE*
"Returns the numerical average value in a dataset, ignoring text."
[& ^{:doc "Dataset item"} items]
(let [tr-flatten-numbers (flatten-numbers (map coerce/excel-number))
items (sequence tr-flatten-numbers items)
len (count items)]
(when-not (zero? len)
(/ (apply math/sum-fn items)
len))))
(def AVERAGE #'AVERAGE*)
(defn COUNT*
"Returns a count of the number of numeric values in a dataset."
[& ^{:doc "Dataset item"} items]
(let [tr-flatten-numbers (flatten-numbers (map (fn [x]
(try
(coerce/excel-number x)
(catch #?(:clj Throwable
:cljs js/Error) _
nil)))))]
(count (sequence tr-flatten-numbers items))))
(def COUNT #'COUNT*)
(defn LENGTH*
"Returns the number of items in the collection."
[^{:doc "Collection"} coll]
(count coll))
(def LENGTH #'LENGTH*)
(defn MAX*
"Returns the maximum value in a numeric dataset."
[& ^{:doc "Dataset item"} items]
(let [tr-coercer (map coerce/excel-number)
tr-flatten-numbers (flatten-numbers tr-coercer)]
(apply max (into [] tr-flatten-numbers items))))
(def MAX #'MAX*)
(defn MIN*
"Returns the minimum value in a numeric dataset."
[& ^{:doc "Dataset item"} items]
(let [tr-coercer (map coerce/excel-number)
tr-flatten-numbers (flatten-numbers tr-coercer)]
(apply min (into [] tr-flatten-numbers items))))
(def MIN #'MIN*)
(def env
{"MIN" MIN
"MAX" MAX
"AVERAGE" AVERAGE
"COUNT" COUNT
"LENGTH" LENGTH})