Iterators and Macros

Callbacks and closures, iterator functions etc. What you need is a way to wrap your code in someone elses code:

for (int i = 0; i < length; i++) {
    struct record rec = records[i];
    // do something interesting with rec
}

Imagine the details of iterating over records is the responsibility of a library:

U: ...
L: for (int i = 0; i < length; i++) {
L:     struct record rec = records[i];
U:     ...
U:     ...
L: }
U: ...

Here "U" means code in the domain of the library user, and "L" means code in the domain of the library. You want the bits of code with the "U" tag to be written by the user, and bits with "L" to be written by the library author.

Perhaps C-style macros can be used (kinda). But pure textual macros (search-replace style) can only get you so far. Lisp AFAIK allows you to pass your internal code into a library function and have it return a new block of code wrapped in the library code.

Can macros be made even more powerful? Can macros be made so powerful that an entire language could be written in macros around machine code? Eg: given an instruction set, and a way of defining macros over the top of them a language could be written.