法拉第扫描功能的来源(https://github.com/ptaoussanis/faraday/blob/master/src/taoensso/faraday.clj#L1197)有一个我正在努力理解的解构形式......
(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}}}]]
...)
Run Code Online (Sandbox Code Playgroud)
那有什么作用:or {span-reqs {:max 5}}?
它是默认值。有关更多详细信息,请参阅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!
Run Code Online (Sandbox Code Playgroud)
使用:keys我们得到
(let [{:keys [a x] :or {x "Not found!"}, :as all} my-map]
(println "I got" a "from" all)
(println "Where is x?" x))
Run Code Online (Sandbox Code Playgroud)
结果相同
:or {span-reqs {:max 5}}指定 iffopts没有密钥:span-reqs,span-reqs将绑定到地图{:max 5}。
请注意,span-reqs不直接引用键:span-reqs- 这也是可能的:
(defn scan
[client-opts table
& [{:keys [attr-conds last-prim-kvs return limit total-segments
filter-expr
segment return-cc?] :as opts
foo :span-reqs
:or {foo {:max 5}}}]])
Run Code Online (Sandbox Code Playgroud)
请注意,在:or映射中,您通常可以为在析构形式中绑定的符号提供默认表达式。
注意:将运行时表达式放入:or. 从 Clojure 1.9-alpha14 开始,无论是否需要它们,它们仍将被评估(参见http://dev.clojure.org/jira/browse/CLJ-1676)。
| 归档时间: |
|
| 查看次数: |
1372 次 |
| 最近记录: |