Say I have a pointer to a function _stack_push(stack* stk, void* el)
. I want to be able to call curry(_stack_push, my_stack)
and get back a function that just takes void* el
. I couldn't think of a way to do it, since C doesn't allow runtime function definition, but I know there are far cleverer people than me here :). Any ideas?
I found a paper by Laurent Dami that discusses currying in C/C++/Objective-C:
More Functional Reusability in C/C++/Objective-c with Curried Functions
Of interest to how it is implemented in C:
Our current implementation uses existing C constructs to add the currying mechanism. This was much easier to do than modifying the compiler, and is sufficient to prove the interest of currying. This approach has two drawbacks, however. First, curried functions cannot be type-checked, and therefore require careful use in order to avoid errors. Second, the curry function cannot know the size of its arguments, and counts them as if they were all of the size of an integer.
The paper does not contain an implementation of curry()
, but you can imagine how it is implemented using function pointers and variadic functions.