1
Name:
Anonymous
2014-04-25 11:23
#include <stdio.h> #include <stdlib.h> void *apply(void *callee, int argc, void *argv) { void **as = (void**)argv; void *(*f)(void *a,...) = (void *(*)(void *a, ...))callee; switch(argc) { case 0: return ((void* (*)())f)(); case 1: return f(as[0]); case 2: return f(as[0], as[1]); case 3: return f(as[0], as[1], as[2]); case 4: return f(as[0], as[1], as[2], as[3]); default: printf("apply: number of arguments limit reached\n"); abort(); } } int main(int argc, char **argv) { char *as[] = {"hello %s\n" , "world"}; apply(printf, 2, as); return 0; }
6
Name:
Anonymous
2014-04-25 14:29
Code is fine except for a minor detail: You have to use
void *() for the type of callee in apply. You can't cast a function pointer to a void pointer. They don't even need to be the same size.
>>5 is impressive!
9
Name:
Anonymous
2014-04-25 14:32
>>7 You can cast to
void *() and call it with an indeterminate number of arguments just fine. Anyway,
currying , man.