store/[org.clojure/clojure "1.8.0"] clj::clojure.core/apply

Community Documentation

Edit

f is a function and the last argument args is a sequence. Calls f with the elements of args as its arguments. If more arguments are specified (x y ...), they are added to the beginning of args to form the complete argument list with which f is called.

Source

(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)))))))))

Example 1

Edit
;; same as (+ 1 2)
(apply + [1 2])
;; => 3

;; same as (+ 1 2 3 4 5 6)

(apply + 1 2 [3 4 5 6])   
;; => 21

;; same as (+)

(apply + [])
;; => 0

Example 2

Edit
;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

Example 3

Edit
;; If you were to try
(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`

(apply max [1 2 3])
;; => 3

;; which is the same as (max 1 2 3)

Example 4

Edit
(apply + [1 2])           ; same as (+ 1 2)
;; => 3

(apply + 1 2 [3 4 5 6])   ; same as (+ 1 2 3 4 5 6)
;; => 21

(apply + [])              ; same as (+)
;; => 0

;; This doesn't work because and is a macro, not a function
(apply and [true false true])
;; => CompilerException java.lang.RuntimeException: Can't take value of a macro: #'clojure.core/and, compiling:(NO_SOURCE_PATH:1:1)

Example 5

Edit
;; Here's an example that uses the optional second argument, args:

(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

(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])' .

Example 6

Edit
;; 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.
(apply and (list true true false true))
;; => ERROR

Example 7

Edit
(def *strings* ["str1" "str2" "str3"])
;; => #'user/*strings*

;; Oops!
(str *strings*)
;; => "[\"str1\" \"str2\" \"str3\"]"

;; Yay!
(apply str *strings*)
;; => "str1str2str3"

;; Note the equivalence of the following two forms
(apply str ["str1" "str2" "str3"])
;; => "str1str2str3"

(str "str1" "str2" "str3")
;; => "str1str2str3"

Example 8

Edit
;; 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

(map #(apply max %) [[1 2 3] [4 5 6] [7 8 9]])
;; => (3 6 9)

Uses on crossclj