# Clojure: The 'apply' function

In my continued playing around with Clojure I came across the ‘apply’ function which is used when we want to call another function with a number of arguments but have actually been given a single argument which contains the argument list.

The example that I’ve been trying to understand is applying ‘str’ to a collection of values.

I started off with the following:

``````
(str [1 2 3])
=> "[1 2 3]"
``````

This just returns the string representation of the vector that we passed it, but what we actually want is to get an output of “123”.

The ‘apply’ function allows us to do that:

``````
(apply str [1 2 3])
=> "123"
``````

That is semantically/conceptually the same as doing this:

``````
(str 1 2 3)
=> "123"
``````

I didn’t quite understand how that could work though and my assumption was that somewhere in the Clojure source the above function call would be happening.

The definition of ‘apply’ is as follows:

``````
(defn apply
"Applies fn f to the argument list formed by prepending args to argseq."
{:arglists '([f args* argseq])}
[#^clojure.lang.IFn f & args]
(. f (applyTo (spread args))))
``````

The first thing which I hadn’t realised is that when you have an ‘&’ before a parameter definition then any arguments provided will be put into a list.

If we break down the example above we end up with the following:

``````
(. str (applyTo (spread [[1 2 3]])))
``````

The ‘spread’ function is defined like so:

``````
(defn spread
{:private true}
[arglist]
(cond
(nil? arglist) nil
(nil? (next arglist)) (seq (first arglist))
:else (cons (first arglist) (spread (next arglist)))))
``````

In this case we only have one item in ‘arglist’ so on line 6 the ‘next arglist’ expression evaluates to nil.

This means that we create a seq from the first argument of the ‘arglist’ which is ‘[1 2 3]‘.

Working our way back up to the ‘apply’ function what we end up with is this:

``````
(. str (applyTo (seq [1 2 3])))
=> "123"
``````

This calls through to an ‘applyTo’ method defined on the ‘clojure.lang.IFn’ interface.

I’m not sure which of the implementations ‘str’ maps to but it seems like the ‘str’ function would eventually be called from the Java code with each of the values in the sequence passed in as a separate argument which is pretty neat!
{"ft_lang_label":"__label__en","ft_lang_prob":0.8720508,"math_prob":0.8992559,"size":2181,"snap":"2019-13-2019-22","text_gpt3_token_len":556,"char_repetition_ratio":0.12953606,"word_repetition_ratio":0.0,"special_character_ratio":0.2696011,"punctuation_ratio":0.07398568,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98385733,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-05-25T01:59:53Z\",\"WARC-Record-ID\":\"<urn:uuid:0ed2dac1-d592-4c1d-b800-60388f96d834>\",\"Content-Length\":\"31156\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d36a90b8-bb92-4f43-889d-8b26c774ec76>\",\"WARC-Concurrent-To\":\"<urn:uuid:e9734fa2-0e28-4a14-8ae7-d198c5612d26>\",\"WARC-IP-Address\":\"185.199.108.153\",\"WARC-Target-URI\":\"https://markhneedham.com/blog/2009/11/25/clojure-the-apply-function/?utm_source=feedburner&amp;utm_medium=feed&amp;utm_campaign=Feed%3A+PlanetTw+%28Planet+TW%29&amp;utm_content=Twitter\",\"WARC-Payload-Digest\":\"sha1:2T4D2AKZREELIHIMOFKM4OGKLKXZN2DA\",\"WARC-Block-Digest\":\"sha1:C2SPWW22IGQHXR5RIMNB5USZBQT5VCQ7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-22/CC-MAIN-2019-22_segments_1558232257845.26_warc_CC-MAIN-20190525004721-20190525030721-00221.warc.gz\"}"}