Get the latest tech news

Quote-unquote "macros"


You’ve probably seen this Python 101 thing before: @memoized def fib(n): if n <= 1: return n return fib(n - 1) + fib(n - 2) Leaving aside the absurdity of computing Fibonacci numbers recursively, it’s a common first introduction to Python decorators and higher-order functions. fib is just a function, and memoized takes that function and returns a new function (or something with a __call__ method) that, you know, memoizes the result. Python’s decorators give us a nice notation for writing this, but we could write the same thing in any dynamic language with first-class functions. Like JavaScript:

I struggle to even think of a situation where you would want a per-callsite(memoize), and in any case it would be easier to write that particular macro without using quote-unquote at all: I’ve been spending some time recently rewriting the Bauble compiler, now that I actually know Janet, and this quote-unquote pattern comes up in just about every nontrivial macro I write. But, well, those are big and complicated and hard to explain and talk about, and I just wanted to point out how I underestimated quote for so long, in case you’ve been balancing your parentheses under the same misconception.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Macros

Macros

Photo of unquote

unquote

Related news:

News photo

101 Macros for Lotus 1-2-3 and Unnamed Lotus Symphony Adventure Game (2022)

News photo

Automata via Macros (2006)

News photo

Dart 3.4: WASM and Macros preview