1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-26 08:20:27 +00:00

update mean

This commit is contained in:
primo-ppcg 2023-09-08 11:35:37 +07:00
parent a13aeaf955
commit 541469371a

View File

@ -643,7 +643,12 @@
(defn mean (defn mean
"Returns the mean of xs. If empty, returns NaN." "Returns the mean of xs. If empty, returns NaN."
[xs] [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 (defn product
"Returns the product of xs. If xs is empty, returns 1." "Returns the product of xs. If xs is empty, returns 1."