code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(27 lines)
1 // This file defines a MAP_LIST(fn, ...) function that applies a function to
2 // every one of the varargs.
3 // For example: baz(MAP_LIST(foo, 1, "x")) -> baz(foo(1), foo("x"))
5 #pragma once
7 #define EVAL0(...) __VA_ARGS__
8 #define EVAL1(...) EVAL0(EVAL0(EVAL0(__VA_ARGS__)))
9 #define EVAL2(...) EVAL1(EVAL1(EVAL1(__VA_ARGS__)))
10 #define EVAL(...) EVAL2(EVAL2(EVAL2(__VA_ARGS__)))
12 #define MAP_END(...)
13 #define MAP_OUT
14 #define MAP_COMMA ,
16 #define MAP_GET_END2() 0, MAP_END
17 #define MAP_GET_END1(...) MAP_GET_END2
18 #define MAP_GET_END(...) MAP_GET_END1
19 #define MAP_NEXT0(test, next, ...) next MAP_OUT
21 #define MAP_LIST_NEXT1(test, next) MAP_NEXT0(test, MAP_COMMA next, 0)
22 #define MAP_LIST_NEXT(test, next) MAP_LIST_NEXT1(MAP_GET_END test, next)
24 #define MAP_LIST0(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST1)(f, peek, __VA_ARGS__)
25 #define MAP_LIST1(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST0)(f, peek, __VA_ARGS__)
27 #define MAP_LIST(f, ...) EVAL(MAP_LIST1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))