Created
February 11, 2015 12:19
-
-
Save laparca/0f7d86f95ce315c9aa7b to your computer and use it in GitHub Desktop.
Pseudo lambda for C
This file contains 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
#inclued <stdio.h> | |
/** | |
* Defines the callback signature | |
*/ | |
typedef int (*callback_t)(void *); | |
/** | |
* Simulates a proccess that inform using a callback | |
* each time it proccess some data. | |
* @param callback Each time a item is proccesed this | |
* function is called | |
*/ | |
void process(callback_t callback) { | |
int i; | |
for (i = 0; i < 10; i++) | |
callback(&i); | |
} | |
int main(void) { | |
/* Call proccess creating a anonymous function that | |
* prints the proccessed elements. This function acts | |
* like a lambda. | |
*/ | |
process( | |
({ | |
int _(void* data) { | |
printf("The value is %d\n", *(int *)data); | |
} | |
/* Return the method pointer */ | |
_; | |
}) | |
); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment