Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Understanding "Apply" In Clojure & ClojureScript

11/30/2017

0 Comments

 
For someone new To Clojure and ClojureScript it can seem like a bit of a "gotcha" to know when you need to use the apply function and when you don't. Hopefully in this post I'll clear up any confusion about how to apply apply!​ Let's lay the foundation for this discussion by looking first at some simple examples.

Reversing A String

Suppose we have a string and want to get a string with the characters in reverse order. Here we have two attempts. The first incorrectly creates a stringified list of reversed character strings, but the second creates the desired string.
(str (reverse "derp"))

;; => "(\"p\" \"r\" \"e\" \"d\")"

(apply str (reverse "derp"))

;; => "pred"
The key thing to remember is that in Clojure is very focused on dealing with sequences, and when you call reverse on a string it returns a sequence of character string in the reverse order. Str is interesting because it can be called on one thing or more things. If called on just one thing then it stringifies the thing and if passed many arguments it will stringify them and then concatenate them. So, this is why the first incorrect version returns what it does. It sees the list of character strings as one thing- a list, and it then nonchalantly returns the stringified version of the whole list. The second example, on the other hand, is saying take the str function and apply it to all the arguments.

One Plus Two

Now let's look at another example where we want to use the + function on a sequence of numbers. If we pass a vector of quoted list of numbers into + we can see that it returns the same sequence back! This is because the + function is implemented tin a way that it takes multiple parameters and sums them.  When we use apply we are telling the compiler to apply the plus function with the contents of the collection as arguments.
(+ [1 2])

;; => [1 2]

(+ '(1 2))

;; => (1 2)

(apply + [1 2])

;; => 3

(apply + '(1 2))

In General

I think that last example illustrates a nice point that we can formalize more generally, and that is that you use apply when you have a collection and a function that receives some number of arguments and you want to call the function with the collection's contents as the arguments. I don't know about you, but for I look at it like that suddenly it ain't so scary! Have fun applying apply! :)
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021