GithubHelp home page GithubHelp logo

Comments (2)

mhuebert avatar mhuebert commented on August 16, 2024 2

Hi @lucywang000, thanks for writing. The nuances of macros in ClojureScript took me a long time to grok. The short answer is that a macro can only be placed in the first position of an s-expression, eg. (+ 1 2). A macro needs to be in this position so that it can 'see' the arguments being passed to it (in this case, 1 and 2), as it is a kind of code-rewriting rule. Functions are more versatile - you can see them 'floating around' as values, such that they have no 'literal' visibility of the arguments they will passed - eg. in (reduce + 0 [1 2 3]), the + can have no awareness that it will be used to sum up the numbers 1, 2 and 3 - that happens only at evaluation-time. Other examples would include partial application (partial + 10), usage with map/filter (map + [1 2 3]), composition (comp pos? (partial + 10)).

In ClojureScript we have the ability to bind the same name to both a macro (for situations where it appears in the first position of an expression) and a function (for all other uses). In my experience this is usually for performance reasons; the macro will do a bit of extra work at compile-time, so that less needs to happen at runtime, or can directly emit some efficient javascript. + is an example of something in the standard library that uses this approach.

from js-interop.

lucywang000 avatar lucywang000 commented on August 16, 2024

@mhuebert Thanks for the explanation. I never used j/get-in as a function argument, looks like it's very flexible in that way!

from js-interop.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.