Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Anonymous Function Shorthand In ClojureScript & Clojure

12/1/2017

0 Comments

 
The shorthand syntax for an anonymous function in Clojure can be a bit confusing (at least it was for me when I first saw it!). In this post I'll go over what it is and show that it's actually not all that scary.

The Dispatch Macro

After a quick look at the docs entry for the dispatch reader macro we see that the # (hashtag / pound / number sign / whatever you want to call it) has quite a few use cases in Clojure, and one of them is for expressing anonymous functions in a more compact way. Indeed, from that link we can see that the 5th bullet point down describes our use case:
- Anonymous function literal (#())
​
#(…​) ⇒ (fn [args] (…​))
where args are determined by the presence of argument literals taking the form %, %n or %&. % is a synonym for %1, %n designates the nth arg (1-based), and %& designates a rest arg. This is not a replacement for fn - idiomatic use would be for very short one-off mapping/filter fns and the like. #() forms cannot be nested.

Nice Clojure Bridge Writeup

This blog post is an excellent writing that describes Clojure's anonymous function syntax to beginners. If you are new and just trying to wrap your head around this anonymous function syntax then i would highly recommend it.

A Quick Example

I'll try to make this example very simple to try to illustrate the point that using the dispatch reader syntax is really doing the same thing as using the "fn" function with a slight difference in how you access arguments passed into the function. In particular, in the shorthand form we use % for the first argument, %n for the nth argument, and %& for "all the rest of the arguments". Here's an example of two Clojure forms that are semantically equivalent.
;; Two different ways to write a function that takes two
;; arguments, stringifies them, and joins them with a space in between.

((fn [a b] (str a " " b)) "derp" 42)
;; => "derp 42"

(#(str % " " %2) "derp" 42)
;; => "derp 42"
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...
    Picture
    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

    March 2023
    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 © 2023