Comments (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.
@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)
- cljdoc build failing because release.clj seems to be included in jar HOT 1
- Destructuring macros HOT 1
- Support for CLJC HOT 1
- ClojureScript as provided dependency HOT 3
- consider using only project name in namespace HOT 4
- Destructuring nested array returns nil HOT 2
- Error getting length of a string HOT 1
- Examples that show the Javascript equivalent HOT 2
- possible new feature: tagged literal, recursive version of #js
- get-in blows up when path does not exists HOT 5
- escaped " does not survive lit HOT 1
- js-delete feature request
- Call a function in a nested key? HOT 10
- How can I translate this js code into cljs code? HOT 3
- a shorter namespace for convenience? HOT 3
- compile error with `j/lit` and fn value HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from js-interop.