Last active
December 25, 2021 23:30
-
-
Save opsJson/0617d98b6897f6d3c2563994d28061b1 to your computer and use it in GitHub Desktop.
Javascript anonymous functions in C.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <unistd.h> | |
| //create a function that recieves a function pointer, and any arguments | |
| void _foo(void (*function)()) { | |
| function("Foo", 555); | |
| } | |
| void _setTimeout(void (*function)(), float time) { | |
| sleep((float)time/1000); | |
| function(); | |
| } | |
| /* define a macro that creates a function with the given parameters and func, then, pass them | |
| to its predefined function */ | |
| #define foo(x, y) {void func x {y} _foo(func);} | |
| #define setTimeout(x, y, z) {void func x {y} _setTimeout(func, z);} | |
| int main() { | |
| foo((char *s, int i), { | |
| printf("%s: %i\n", s, i); | |
| }); | |
| setTimeout((), { | |
| printf("Hello, world!\n"); | |
| }, 3000); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment