"🔒"
(def B 1e9)
#Result
##'user/B

"🔒"
(def social-network
    {:members [ {:id 1000
                 :name "Jack"
                 :age 20
                 :hobbies ["sport", "reading"]}
               {:id 1001
                :name "Joe"
                :occupation "electrician"}
               {:id 1002
                :name "Elon"
                :occupation "CEO"
                :hobbies ["space" "coding"]}]
     :companies [{:name "GM"
                  :size 157000
                  :market-cap (* 55 B)}
                 {:name "Twitter"
                  :size 7000
                  :market-cap (* 41 B)}
                 {:name "Tesla"
                  :size 2500
                  :market-cap (* 30 B)}
                 {:name "Spotify"
                  :size 6000
                  :market-cap (* 15 B)}
                 ]
     :connections [{:person-id 1000
                    :company "Twitter"
                    :type :use
                    }
                   {:person-id 1002 :company "Tesla" :type :ceo}
                   {:person-id 1001
                    :company "Twitter"
                    :type :owns}
                   {:person-id 1002
                    :company "GM"
                    :type :works}]
    })
#Result
##'user/social-network

"🔒"
(require '[clojure.pprint :refer [pprint]])
(pprint social-network)
#Result
#{:members
# [{:id 1000, :name "Jack", :age 20, :hobbies ["sport" "reading"]}
#  {:id 1001, :name "Joe", :occupation "electrician"}
#  {:id 1002,
#   :name "Elon",
#   :occupation "CEO",
#   :hobbies ["space" "coding"]}],
# :companies
# [{:name "GM", :size 157000, :market-cap 5.5E10}
#  {:name "Twitter", :size 7000, :market-cap 4.1E10}
#  {:name "Tesla", :size 2500, :market-cap 3.0E10}
#  {:name "Spotify", :size 6000, :market-cap 1.5E10}],
# :connections
# [{:person-id 1000, :company "Twitter", :type :use}
#  {:person-id 1002, :company "Tesla", :type :ceo}
#  {:person-id 1001, :company "Twitter", :type :owns}
#  {:person-id 1002, :company "GM", :type :works}]}
#nil

"🔒"
; compute the average value per employee
; using for-loop
(let [companies (social-network :companies)
      total-size (apply + (for [company companies] (:size company)))
      total-market-cap (apply + (for [company companies] (:market-cap company)))]
    (/ total-market-cap total-size))
#Result
#817391.304347826

"🔒"
; using map

(/ (apply + (map :market-cap (social-network :companies)))
   (apply + (map :size (social-network :companies))))
#Result
#817391.304347826

"🔒"
; use reduce
(as-> (reduce (fn [state company] {:total-market-cap (+ (:total-market-cap state) (:market-cap company))
                                   :total-size (+ (:total-size state) (:size company))})
        {:total-market-cap 0
         :total-size 0}
        (social-network :companies))
    $
    (/ (:total-market-cap $)
       (:total-size $)))
#Result
#817391.304347826

"🔒"
(defn add-by-attr [state attr company]
    (update state attr (fn [x] (+ x (get company attr)))))
#Result
##'user/add-by-attr

"🔒"
(as-> (reduce (fn [state company]
                (-> state (add-by-attr :market-cap company) (add-by-attr :size company)))
            {:market-cap 0 :size 0}
            (social-network :companies))
    $
    (/ (:market-cap $) (:size $)))
#Result
#817391.304347826

"🔒"
(defn id-of [name]
    (->> social-network
        (:members)
        (filter (fn [member] (= (:name member) name)))
        (first)
        (:id)))

(defn connections-of [id]
    (->> social-network
        (:connections)
        (filter (fn [conn] (= (:person-id conn) id)))))

(defn info-of [company-name]
    (->> social-network
        (:companies)
        (filter (fn [company] (= (:name company) company-name)))
        (first)))
#Result
##'user/info-of

"🔒"
(->> "Elon" (id-of) (connections-of) (map :company) (map info-of))
#Result
#({:name "Tesla", :size 2500, :market-cap 3.0E10} {:name "GM", :size 157000, :market-cap 5.5E10})


"🔒"
(as->
    (->> "Elon" (id-of) (connections-of) (map :company) (map info-of))
    $
    (reduce (fn [state company]
                (-> state (add-by-attr :market-cap company) (add-by-attr :size company)))
            {:market-cap 0 :size 0}
            $)
    (/ (:market-cap $) (:size $)))
#Result
#532915.3605015674

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: