C语言实现的mapreduce小程序
#include
typedef int (*mapFunction)(int);
typedef int (*reduceFunction)(int,int);
#define ERROR -1
void map(mapFunction func, int *list, int len)
{
int i;
for(i=0; i
{
list[i]=func(list[i]);
}
}
int reduce(reduceFunction func, int *list, int len)
{
if(len <= 0)
{
return ERROR;
}
int retVal = 0;
int i;
for(i=0; i
{
retVal=func(retVal, list[i]);
}
return retVal;
}
int square(int i)
{
return i*i;
}
int add(int i, int j)
{
return i+j;
}
int main(int argc, char* argv[])
{
int array[5];
int i;
for(i=0; i<5; i++)
{
array[i]=i;
}
mapFunction mapFuncPointer = (mapFunction)□
reduceFunction reduceFuncPointer = (reduceFunction)&add;
map(mapFuncPointer, array, 5);
int result = reduce(reduceFuncPointer, array, 5);
printf("The result is %d\n", result);
return 0;
}