I'm writing a Linked List implementation and want to create a foreach
function. I want to have two versions of this function: one for internal use that iterates over the nodes themselves, and one for the end user that iterates over the data held in the nodes. Since the code will be nearly the same for each, I'd like to define the second version in terms of the first. Right now, I have two typedef
s for the functions used to iterate in each version, and the internal version of foreach:
// The internal-use version that takes a node
typedef void(*Foreach_Node_Function)(size_t index, Node* node);
// The end-user version that takes raw data
typedef void(*Foreach_Function)(size_t index, void* data);
void static foreach_node(Linked_List* ll, Foreach_Node_Function f) {
Node* current = ll->head;
for (size_t i = 0; i < length(ll); i++) {
(*f)(i, current);
current = current->next;
}
}
I need to be able to write an adapter that turns a Foreach_Node_Function
into a Foreach_Function
. If I had access to anonymous functions, this would be trivial. I could have a function close over a Foreach_Function
and modify the given data before passing it along. Something like (pretending lambda
is a thing):
void foreach(Linked_List* ll, Foreach_Function f) {
Foreach_Node_Function adapted = lambda(size_t i, Node* node) {
// Only pass on the data to f
(*f)(i, node->data);
};
foreach_node(ll, adapted);
}
C doesn't seem to support anonymous functions though unless you're willing to resort to compiler specific hacks, and I can't see how this could possibly be achieved without them.
Is this possible to achieve in C, and if so, how?