update `mean`

This commit is contained in:
primo-ppcg 2023-09-08 11:35:37 +07:00
parent a13aeaf955
commit 541469371a
1 changed files with 6 additions and 1 deletions

View File

@ -643,7 +643,12 @@
(defn mean
"Returns the mean of xs. If empty, returns NaN."
[xs]
(/ (sum xs) (length xs)))
(if (lengthable? xs)
(/ (sum xs) (length xs))
(do
(var [accum total] [0 0])
(each x xs (+= accum x) (++ total))
(/ accum total))))
(defn product
"Returns the product of xs. If xs is empty, returns 1."