mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
core: sort->sort-with; fix statistics with new sorter
This commit is contained in:
parent
eba07aeb34
commit
4d6b211853
@ -118,4 +118,7 @@
|
||||
|
||||
;(defmacro push-back! [a e]
|
||||
; (list 'set! a (list 'Array.push-back (list 'copy a) e)))
|
||||
|
||||
(defn sort [a]
|
||||
(sort-with a cmp))
|
||||
)
|
||||
|
@ -17,9 +17,6 @@
|
||||
iqr Double
|
||||
])
|
||||
|
||||
(defn sorter [a b]
|
||||
(to-int (- @a @b)))
|
||||
|
||||
(defn sum [data]
|
||||
(if (= 0 (Array.count data))
|
||||
0.0
|
||||
@ -76,7 +73,7 @@
|
||||
|
||||
(defn median [data]
|
||||
(let [n (Array.count data)
|
||||
sorted (Array.copy (Array.sort data sorter))]
|
||||
sorted (Array.sort @data)]
|
||||
(cond (= n 0) 0.0
|
||||
(= (mod n 2) 1) @(Array.nth data (/ n 2))
|
||||
(let [mid (/ n 2)] ; else
|
||||
@ -86,21 +83,21 @@
|
||||
|
||||
(defn low-median [data]
|
||||
(let [n (Array.count data)
|
||||
sorted (Array.copy (Array.sort data sorter))]
|
||||
sorted (Array.sort @data)]
|
||||
(cond (= n 0) 0.0
|
||||
(= (mod n 2) 1) @(Array.nth data (/ n 2))
|
||||
@(Array.nth data (dec (/ n 2)))))) ; else
|
||||
|
||||
(defn high-median [data]
|
||||
(let [n (Array.count data)
|
||||
sorted (Array.copy (Array.sort data sorter))]
|
||||
sorted (Array.sort @data)]
|
||||
(if (= n 0)
|
||||
0.0
|
||||
@(Array.nth data (/ n 2)))))
|
||||
|
||||
(defn grouped-median [data interval]
|
||||
(let [n (Array.count data)
|
||||
sorted (Array.copy (Array.sort data sorter))]
|
||||
sorted (Array.sort @data)]
|
||||
(cond (= n 0) 0.0
|
||||
(= n 1) @(Array.nth data 0)
|
||||
(let [x @(Array.nth data (/ n 2)) ; else
|
||||
@ -159,13 +156,13 @@
|
||||
(Double.+ lo (Double.* d (Double.- hi lo))))))
|
||||
|
||||
(defn quartiles [data]
|
||||
(let [tmp (Array.sort data sorter)
|
||||
(let [tmp (Array.sort @data)
|
||||
first 25.0
|
||||
second 50.0
|
||||
third 75.0
|
||||
a (percentile-of-sorted tmp first)
|
||||
b (percentile-of-sorted tmp second)
|
||||
c (percentile-of-sorted tmp third)]
|
||||
a (percentile-of-sorted &tmp first)
|
||||
b (percentile-of-sorted &tmp second)
|
||||
c (percentile-of-sorted &tmp third)]
|
||||
[a b c]))
|
||||
|
||||
(defn iqr [data]
|
||||
@ -173,7 +170,7 @@
|
||||
(the Double (- @(Array.nth s 2) @(Array.nth s 0)))))
|
||||
|
||||
(defn winsorize [samples pct]
|
||||
(let [tmp (the (Ref (Array Double)) (Array.sort samples sorter))
|
||||
(let [tmp &(Array.sort @samples)
|
||||
lo (Statistics.percentile-of-sorted tmp pct)
|
||||
hi (Statistics.percentile-of-sorted tmp (Double.- 100.0 pct))]
|
||||
(do
|
||||
|
@ -169,7 +169,7 @@ templateNth =
|
||||
|
||||
templateSort :: (String, Binder)
|
||||
templateSort = defineTypeParameterizedTemplate templateCreator path t
|
||||
where path = (SymPath ["Array"] "sort")
|
||||
where path = (SymPath ["Array"] "sort-with")
|
||||
vt = VarTy "t"
|
||||
t = (FuncTy [StructTy "Array" [vt], FuncTy [RefTy vt, RefTy vt] IntTy] (StructTy "Array" [vt]))
|
||||
templateCreator = TemplateCreator $
|
||||
|
@ -93,7 +93,7 @@
|
||||
)
|
||||
(assert-equal test
|
||||
&[1 2 3 4 5 6 7 8 9]
|
||||
&(sort (range 9 1 -1) cmp)
|
||||
&(sort (range 9 1 -1))
|
||||
"sort works as expected"
|
||||
)
|
||||
(assert-equal test
|
||||
|
Loading…
Reference in New Issue
Block a user