Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does :or mean in Clojure destructuring?

Tags:

clojure

The source for faraday's scan function (https://github.com/ptaoussanis/faraday/blob/master/src/taoensso/faraday.clj#L1197) has a destructuring form that I am struggling to understand...

(source far/scan)
(defn scan
  "..."
  [client-opts table
   & [{:keys [attr-conds last-prim-kvs span-reqs return limit total-segments
              filter-expr
              segment return-cc?] :as opts
       :or   {span-reqs {:max 5}}}]]
...)

What does that :or {span-reqs {:max 5}} do?

like image 422
Bob Kuhar Avatar asked Dec 08 '16 20:12

Bob Kuhar


1 Answers

It is the default value. For more details please see http://clojure.org/guides/destructuring

(def my-map {:a "A" :b "B" :c 3 :d 4})
(let [{a :a, x :x, :or {x "Not found!"}, :as all} my-map]
  (println "I got" a "from" all)
  (println "Where is x?" x))
;= I got A from {:a "A" :b "B" :c 3 :d 4}
;= Where is x? Not found!

using :keys we get

(let [{:keys [a x] :or {x "Not found!"}, :as all} my-map]
  (println "I got" a "from" all)
  (println "Where is x?" x))

with the same result

like image 146
Alan Thompson Avatar answered Nov 15 '22 00:11

Alan Thompson