add and test helper functions
This commit is contained in:
parent
f763db91a2
commit
22b7067f10
@ -107,3 +107,8 @@
|
||||
[x]
|
||||
#?(:clj (Math/tan x)
|
||||
:cljs (js/Math.tan x)))
|
||||
|
||||
(defn transpose
|
||||
"returns a vector containing the transpose of a coll of colls"
|
||||
[x]
|
||||
(apply map vector x))
|
@ -1,11 +1,26 @@
|
||||
(ns propeller.tools.metrics
|
||||
(:require [propeller.tools.math :as math]))
|
||||
|
||||
(defn argmax
|
||||
"returns the index of the maximum value in a list"
|
||||
[coll]
|
||||
;(prn {:func :argmax :coll coll})
|
||||
(->> coll
|
||||
(map-indexed vector)
|
||||
(apply max-key second)
|
||||
first))
|
||||
|
||||
(defn mean
|
||||
"Returns the mean of a collection."
|
||||
[coll]
|
||||
(if (empty? coll) 0 (math/div (apply + coll) (count coll))))
|
||||
|
||||
(defn mean-of-colls
|
||||
"returns the mean of multiple colls"
|
||||
[coll]
|
||||
;(prn {:func :mean-of-colls :coll coll})
|
||||
(map mean (math/transpose coll)))
|
||||
|
||||
(defn median
|
||||
"Returns the median of a collection."
|
||||
[coll]
|
||||
|
@ -1,6 +1,21 @@
|
||||
(ns propeller.utils
|
||||
(:require [clojure.zip :as zip]))
|
||||
|
||||
(defn filter-by-index
|
||||
"filters a collection by a list of indices"
|
||||
[coll idxs]
|
||||
;(prn {:func :filter-by-index :coll coll :idxs idxs})
|
||||
(keep-indexed #(when ((set idxs) %1) %2)
|
||||
coll))
|
||||
|
||||
(defn drop-nth
|
||||
"drops the nth element from a collection"
|
||||
[n coll]
|
||||
;(prn {:func :drop-nth :n n :coll coll})
|
||||
(concat
|
||||
(take n coll)
|
||||
(nthrest coll (inc n))))
|
||||
|
||||
(defn first-non-nil
|
||||
"Returns the first non-nil values from the collection, or returns `nil` if
|
||||
the collection is empty or only contains `nil`."
|
||||
|
@ -3,6 +3,17 @@
|
||||
[propeller.tools.metrics :as m]
|
||||
[propeller.tools.math :as a]))
|
||||
|
||||
(t/deftest argmax-test
|
||||
(t/is (= (m/argmax '(1 2 3 4)) 3))
|
||||
(t/is (= 1 (nth '(1 1 1 1) (m/argmax '(1 1 1 1)))))
|
||||
(t/is (= 3 (nth '(1 3 3 1) (m/argmax '(1 3 3 1)))))
|
||||
(t/is (= 0 (m/argmax '(1)))))
|
||||
|
||||
(t/deftest mean-of-colls-test
|
||||
(t/is (= (m/mean-of-colls '((1 2 3 4) (4 3 2 1))) '(2.5 2.5 2.5 2.5)))
|
||||
(t/is (= (m/mean-of-colls '((1 2 3) (4 3 2 1))) '(2.5 2.5 2.5)))
|
||||
(t/is (= (m/mean-of-colls '((1))) '(1.0))))
|
||||
|
||||
(t/deftest mean-test
|
||||
(t/is (= (m/mean '(1 2 3 4)) 2.5))
|
||||
(t/is (= (m/mean '()) 0)))
|
||||
@ -15,7 +26,7 @@
|
||||
|
||||
(t/deftest levenshtein-distance-test
|
||||
(t/is (= (m/levenshtein-distance "kitten" "sipping") 5))
|
||||
(t/is (= (m/levenshtein-distance "" "hello")) 5))
|
||||
(t/is (= (m/levenshtein-distance "" "hello") 5)))
|
||||
|
||||
(t/deftest sequence-similarity-test
|
||||
(t/is (a/approx= (m/sequence-similarity "kitten" "sipping") 0.2857 0.001))
|
||||
|
Loading…
x
Reference in New Issue
Block a user