J
jacob navia
Hi
I am writing a container library in C. I have developed iterators, and
generic containers, and to test my library I wrote the lisp function
"mapcar".
Mapcar takes a list and builds a list containing the result of a monadic
function applied to each element of the input list.
For instance
(mapcar #'abs (1 -2 3 -4 5 -6 7))
will produce
(1 2 3 4 5 6 7)
i.e. the function 'abs' will be applied to each element.
Within my software, mapcar looks like this:
int mapcar(SequentialContainer *src, /* The source container */
void *(*fn)(void *),/* Function to call with each element */
SequentialContainer *result) /* The resulting container */
{
Iterator *it = iSequentialContainer.newIterator(src);
int r=1;
void *obj;
if (it == NULL)
return CONTAINER_ERROR_NOMEMORY;
for (obj = it->GetFirst(it);
obj != NULL;
obj = it->GetNext(it)) {
void *tmp = fn(obj);
int r = iSequentialContainer.Add(result,tmp);
if (r < 0) {
/* In case of any error return a partial result
and the error code */
break;
}
}
deleteIterator(it);
return r;
}
"SequentialContainer" is an abstract class that includes lists, arrays,
stringcollections, and all containers that have a natural sequence
(index) that follows the natural integers. The result of "mapcar" then,
will be of the same actual type that the "result" container passed to
mapcar.
Now (at last) my question:
What would be the corresponding C++ code?
What would be the best way of implementing it?
Thanks in advance to all C++ wizards that take this challenge.
jacob
I am writing a container library in C. I have developed iterators, and
generic containers, and to test my library I wrote the lisp function
"mapcar".
Mapcar takes a list and builds a list containing the result of a monadic
function applied to each element of the input list.
For instance
(mapcar #'abs (1 -2 3 -4 5 -6 7))
will produce
(1 2 3 4 5 6 7)
i.e. the function 'abs' will be applied to each element.
Within my software, mapcar looks like this:
int mapcar(SequentialContainer *src, /* The source container */
void *(*fn)(void *),/* Function to call with each element */
SequentialContainer *result) /* The resulting container */
{
Iterator *it = iSequentialContainer.newIterator(src);
int r=1;
void *obj;
if (it == NULL)
return CONTAINER_ERROR_NOMEMORY;
for (obj = it->GetFirst(it);
obj != NULL;
obj = it->GetNext(it)) {
void *tmp = fn(obj);
int r = iSequentialContainer.Add(result,tmp);
if (r < 0) {
/* In case of any error return a partial result
and the error code */
break;
}
}
deleteIterator(it);
return r;
}
"SequentialContainer" is an abstract class that includes lists, arrays,
stringcollections, and all containers that have a natural sequence
(index) that follows the natural integers. The result of "mapcar" then,
will be of the same actual type that the "result" container passed to
mapcar.
Now (at last) my question:
What would be the corresponding C++ code?
What would be the best way of implementing it?
Thanks in advance to all C++ wizards that take this challenge.
jacob