Skip to content
Snippets Groups Projects
Commit 71e7f697 authored by abivarma.kandiah's avatar abivarma.kandiah
Browse files

add Vector Reduce

parent 32fc7414
Branches
No related tags found
No related merge requests found
...@@ -32,5 +32,6 @@ void vector_free(vector vec); ...@@ -32,5 +32,6 @@ void vector_free(vector vec);
void vector_print(vector vec, void (*print)(type)); void vector_print(vector vec, void (*print)(type));
vector vector_map(vector vec, type (*f)(type)); vector vector_map(vector vec, type (*f)(type));
vector vector_filter(vector vec, bool (*f)(type)); vector vector_filter(vector vec, bool (*f)(type));
type vector_reduce(vector vec, type neutral, type (*f)(type, type));
#endif #endif
\ No newline at end of file
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
void print_type(type val); void print_type(type val);
int times_two(type a); int times_two(type a);
bool lower_than_five(type a); bool lower_than_five(type a);
type add(type lhs, type rhs);
int main() int main()
{ {
...@@ -71,6 +72,11 @@ int main() ...@@ -71,6 +72,11 @@ int main()
vector_print(test_3, &print_type); vector_print(test_3, &print_type);
printf("\n"); printf("\n");
//Test Vector Reduce
type sum = vector_reduce(test_3, 0, add);
print_type(sum);
printf("\n");
//Test Vector Free //Test Vector Free
vector_free(test); vector_free(test);
vector_free(test_2); vector_free(test_2);
...@@ -93,3 +99,8 @@ bool lower_than_five(type a) ...@@ -93,3 +99,8 @@ bool lower_than_five(type a)
{ {
return (a < 5); return (a < 5);
} }
type add(type lhs, type rhs)
{
return lhs + rhs;
}
\ No newline at end of file
...@@ -159,3 +159,12 @@ vector vector_filter(vector vec, bool (*f)(type)) ...@@ -159,3 +159,12 @@ vector vector_filter(vector vec, bool (*f)(type))
} }
return new_vec; return new_vec;
} }
type vector_reduce(vector vec, type neutral, type (*f)(type, type))
{
for (int i = 0; i < vec->length; i++)
{
neutral = f(neutral, vec->content[i]);
}
return neutral;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment