Gathering stats about ages
Now that we can safely calculate the average age of a number of pirates, it might be interesting to take this further and calculate the median and standard deviation of the pirates' ages, in addition to their average age.
We already have a function to calculate the average; so, let's just create the ones to calculate the median and the standard deviation of a list of numbers, as follows:
(defn median [& ns] (let [ns (sort ns) cnt (count ns) mid (bit-shift-right cnt 1)] (if (odd? cnt) (nth ns mid) (/ (+ (nth ns mid) (nth ns (dec mid))) 2)))) (defn std-dev [& samples] (let [n (count samples) mean (/ (reduce + samples) n) intermediate (map #(Math/pow (- %1 mean) 2) samples)] (Math/sqrt (/ (reduce + intermediate) n))))
With these functions in place, we can write the code that will gather all of the stats for us:
(let [a (some-> (pirate-by-name "Jack Sparrow") age) b (some-> (pirate-by-name "Blackbeard") age) c (some-> (pirate-by-name "Hector Barbossa") age) avg (avg a b c) median (median a b c) std-dev (std-dev a b c)] {:avg avg :median median :std-dev std-dev}) ;; {:avg 56.666668, ;; :median 60, ;; :std-dev 12.472191289246473}
This implementation is fairly straightforward. First, we retrieve all of the ages that we're interested in and bind them to the locals a
, b
, and c
. We then reuse the values when calculating the remaining stats. Finally, we gather all of the results in map
, for easy access.
By now, you probably know where we're headed. What if any of those values is nil
? Consider the following code:
(let [a (some-> (pirate-by-name "Jack Sparrow") age) b (some-> (pirate-by-name "Davy Jones") age) c (some-> (pirate-by-name "Hector Barbossa") age) avg (avg a b c) median (median a b c) std-dev (std-dev a b c)] {:avg avg :median median :std-dev std-dev}) ;; NullPointerException clojure.lang.Numbers.ops (Numbers.java:961)
The second binding, b
, returns nil
, as we don't have any information about Davy Jones. As such, it causes the calculations to fail. Like before, we can change our implementation to protect us from such failures, as follows:
(let [a (some-> (pirate-by-name "Jack Sparrow") age) b (some-> (pirate-by-name "Davy Jones") age) c (some-> (pirate-by-name "Hector Barbossa") age) avg (when (and a b c) (avg a b c)) median (when (and a b c) (median a b c)) std-dev (when (and a b c) (std-dev a b c))] (when (and a b c) {:avg avg :median median :std-dev std-dev})) ;; nil
This time, it's even worse than when we only had to calculate the average. The code is checking for nil
values in four extra spots—before calling the three stats functions, and just before gathering the stats into the resulting map
.
Can we do better?