Skip to content

Instantly share code, notes, and snippets.

@laparca
Created February 11, 2015 12:19
Show Gist options
  • Save laparca/0f7d86f95ce315c9aa7b to your computer and use it in GitHub Desktop.
Save laparca/0f7d86f95ce315c9aa7b to your computer and use it in GitHub Desktop.
Pseudo lambda for C
#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