user=> (def *strings* ["str1" "str2" "str3"]) #'user/*strings* ;; Oops! user=> (str *strings*) "[\"str1\" \"str2\" \"str3\"]" ;; Yay! user=> (apply str *strings*) "str1str2str3" user=> ;; Note the equivalence of the following two forms user=> (apply str ["str1" "str2" "str3"]) "str1str2str3" user=> (str "str1" "str2" "str3") "str1str2str3"
;; If you were to try user=> (max [1 2 3]) [1 2 3] ;; You would get '[1 2 3]' for the result. In this case, 'max' has received one ;; vector argument, and the largest of its arguments is that single vector. ;; If you would like to find the largest item **within** the vector, you would need ;; to use `apply` user=> (apply max [1 2 3]) 3 ;; which is the same as (max 1 2 3)
;; Here's an example that uses the optional second argument, args: user=> (apply map vector [[:a :b] [:c :d]]) ([:a :c] [:b :d]) ;; In this example, 'f' = 'map', 'args' = 'vector', and argseq = '[:a :b] [:c :d]', ;; making the above code equivalent to user=> (map vector [:a :b] [:c :d]) ([:a :c] [:b :d]) ;Same answer as above ;; It might help to think of 'map' and 'vector' "slipping inside" the argument list ;; ( '[[:a :b] [:c :d]]' ) to give '[map vector [:a :b] [:c :d]]' , which then ;; becomes the executable form '(map vector [:a :b] [:c :d])' .
;; only functions can be used with apply. 'and' is a macro ;; because it needs to evaluate its arguments lazily and so ;; does not work with apply. user=> (apply and (list true true false true) -> ERROR
;apply is used to apply an operator to its operands. (apply + '(1 2)) ; equal to (+ 1 2) => 3 ;you can also put operands before the list of operands and they'll be consumed in the list of operands (apply + 1 2 '(3 4)) ; equal to (apply + '(1 2 3 4)) => 10
;; You can use map and apply together to drill one level deep in a collection ;; of collections, in this case returning a collection of the max of each ;; nested collection user=> (map #(apply max %) [[1 2 3][4 5 6][7 8 9]]) (3 6 9)
;; Using `apply` with optional keyword parameters: (defn add2 [a & {:keys [plus] :or {plus 0}}] (+ 2 plus a)) (add2 4) ; => 6 (add2 4 :plus 1) ; => 7 (apply add2 [4]) ; => 6 (apply add2 [4 {:plus 1}]) ; => IllegalArgumentException (apply add2 [4 :plus 1]) ; => 7
This is evil. Don't ever use it. It makes a macro behave like a f
(defn apply "Applies fn f to the argument list formed by prepending intervening arguments to args." {:added "1.0" :static true} ([^clojure.lang.IFn f args] (. f (applyTo (seq args)))) ([^clojure.lang.IFn f x args] (. f (applyTo (list* x args)))) ([^clojure.lang.IFn f x y args] (. f (applyTo (list* x y args)))) ([^clojure.lang.IFn f x y z args] (. f (applyTo (list* x y z args)))) ([^clojure.lang.IFn f a b c d & args] (. f (applyTo (cons a (cons b (cons c (cons d (spread args)))))))))
Comments top
No comments for apply. Log in to add a comment.