Generalized Threading Macro in Clojure

There is now a generalized threading macro in Clojure since 1.5 called as->. This tweet gives an example of how it works: https://twitter.com/borkdude/status/302881431649128448 (as-> “/tmp” x (java.io.File. x) (file-seq x) (filter (memfn isDirectory) x) (count x)) First ‘x’ is bound to “/tmp” and a file is made out of it. ‘x’ is rebound again to … Read more

Does Q_UNUSED have any side effects?

No in many cases (e.g. just passing a simple variable to the macro). The definition is inside qglobal.h: # define Q_UNUSED(x) (void)x; To disable unused variable warnings. You can use the variable after this macro without any problem. However, if you pass an expression or something else to the macro and the compiler has to … Read more

Macros to create strings in C

In C, string literals are concatenated automatically. For example, const char * s1 = “foo” “bar”; const char * s2 = “foobar”; s1 and s2 are the same string. So, for your problem, the answer (without token pasting) is #ifdef __TESTING #define IV_DOMAIN “example.org” #elif __LIVE_TESTING #define IV_DOMAIN “test.example.com” #else #define IV_DOMAIN “example.com” #endif #define … Read more

Implement generic swap macro in C [duplicate]

This works well only with integers. For floats it will fail (e.g. try running it with a very large float and a very small one). I would suggest something as follows: #define swap(x,y) do \ { unsigned char swap_temp[sizeof(x) == sizeof(y) ? (signed)sizeof(x) : -1]; \ memcpy(swap_temp,&y,sizeof(x)); \ memcpy(&y,&x, sizeof(x)); \ memcpy(&x,swap_temp,sizeof(x)); \ } while(0) … Read more

Mathematica: Unevaluated vs Defer vs Hold vs HoldForm vs HoldAllComplete vs etc etc

These are pretty tricky constructs, and it’s tough to give clear explanations; they aren’t as straightforward as Lisp macros (or, for that matter, the relationship between Lisp’s QUOTE and EVAL). However, there’s a good, lengthy discussion available in the form of notes from Robby Villegas’s 1999 talk “Unevaluated Expressions” on Wolfram’s website. Defer is omitted … Read more

How do you implement header guards, and what can you put between them?

The FILENAME_H is a convention. If you really wanted, you could use #ifndef FLUFFY_KITTENS as a header guard (provided it was not defined anywhere else), but that would be a tricky bug if you defined it somewhere else, say as the number of kittens for something or other. In the header file add.h the declarations … Read more

What does the tt metavariable type mean in Rust macros?

That’s a notion introduced to ensure that whatever is in a macro invocation correctly matches (), [] and {} pairs. tt will match any single token or any pair of parenthesis/brackets/braces with their content. For example, for the following program: fn main() { println!(“Hello world!”); } The token trees would be: fn main () ∅ … Read more