Fork me on GitHub
#code-reviews
<
2021-05-09
>
raspasov05:05:10

Another solution for the socks (using frequencies, a bit higher level, avoiding explicit reduce):

(->> 
 [1 2 1 2 1 3 2] 
 (frequencies)
 (map
  (fn [[sock-color cnt]]
   ;determine if we have an odd or even number of socks for each color
   (let [cnt' (if (odd? cnt) (dec cnt) cnt)]
    ;number of pairs of socks for a color
    (/ cnt' 2))))
 ;total of all pairs
 (apply +))

raspasov06:05:33

Second one, less imperative (using a library for dedupe-by):

(count
 (sequence
  (comp
   ;dedupe by below sea level
   (medley.core/dedupe-by #(neg? %))
   ;at sea level
   (filter zero?))
  (reductions + [+1 -1 -1 -1 +1 -1 +1 +1])))