In this section, we will explore a different use case for the option functor. We would like to, given a number of pirates, calculate the average of their ages. This is simple enough to do:
(defn avg [& xs] (float (/ (apply + xs) (count xs)))) (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 a b c)) ;; 56.666668
Note that we are using some-> here, to protect us from nil values. Now, what happens if there is a pirate for which we have no information? Consider the following code snippet:
(let [a (some-> (pirate-by-name "Jack Sparrow") age) b (some-> (pirate-by-name "Davy Jones") age) c (some-> (pirate-by-name "Hector Barbossa")...