Binds name to expr, evaluates the first form in the lexical context of that binding, then binds name to that result, repeating for each successive form, returning the result of the last form.
(def owners [{:owner "Jimmy" :pets (ref [{:name "Rex" :type :dog} {:name "Sniffles" :type :hamster}])} {:owner "Jacky" :pets (ref [{:name "Spot" :type :mink} {:name "Puff" :type :magic-dragon}])}]) ;; This example is contrived as there are other more ;; terse ways of expressing the idea. It demonstrates ;; several of the ways to extract items from a collection. ;; Notice how the collection can be used in function or ;; parameter position. (as-> owners $ (nth $ 0) (:pets $) (deref $) ($ 1) ($ :type)) ;;=> :hamster
(as-> 0 n (inc n) ; n is 0 here passed from first parameter to as-> (inc n)) ; n is 1 here passed from result of previous inc expression ;;=> 2
; use it in the middle of a -> pipeline to sprinkle in some flexibility (-> [10 11] (conj 12) (as-> xs (map - xs [3 2 1])) (reverse)) ; (11 9 7)
;; when you want to use arbitrary positioning of your argument in a thread macro (as-> {:a 1 :b 2} m (update m :a + 10) (reduce (fn [s [_ v]] (+ s v)) 0 m)) ;; when you'd like an if statement in your thread (as-> {:a 1 :b 2} m (update m :a + 10) (if update-b (update m :b + 10) m))
;; as-> with destructured binding (let [req {:host "//mysite.com" :path "/a/123" :x "15.1" :y "84.2"}] (as-> req {:keys [host path x y] :as m} (assoc m :url (str host path)) (assoc m :coord [(Double/valueOf x) (Double/valueOf y)]))) ;; {:host "//mysite.com" :path "/a/123" :x "15.1" :y "84.2" :url "//mysite.com/a/123" :coord [15.1 84.2]}
Threads the expr through the forms. Inserts x as the second item in the first form, making a list ...
Threads the expr through the forms. Inserts x as the last item in the first form, making a list of...
Takes an expression and a set of test/form pairs. Threads expr (via ->) through each form for whic...
Takes an expression and a set of test/form pairs. Threads expr (via ->>) through each form for whi...
When expr is not nil, threads it into the first form (via ->), and when that result is not nil, th...
When expr is not nil, threads it into the first form (via ->>), and when that result is not nil, t...
as->