Question
Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you will be implementing all three, along with three minor
Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you will be implementing all three, along with three minor functions that can be passed to them.
Map
The map function takes as arguments a function pointer and a integer vector pointer. It applies the function to every element in the vector, storing the results in-order in a new vector. It then returns a pointer to a new vector.
You should pass your square function into map to test it.
Sample Input { 1, 3, 5, 7, 9 }
Expected Output { 1, 9, 25, 49, 81 }
Filter
The filter function takes as arguments a function pointer and a integer vector pointer. It tests every element in the vector against the function, storing in a new vector those elements for which the function returns true. It then returns a pointer to the new vector.
You should pass your isEven function into filter to test it.
Sample Input { 1, 2, 3, 4, 5 }
Expected Output { 2, 4 }
Reduce
The reduce function takes as arguments a function pointer and a integer vector pointer. The reduce function reduces a vector down to a single value by passing a running total and next vector element to the function, then storing the results as the new total. It does this for every element of the vector.
Sample Input { 1, 2, 3, 4 }
Expected Output 24
You should pass your product function into reduce to test it
C++, this is the given code
#include
#include
using namespace std;
vector
vector
int reduce ( int (*fxn) (int, int), vector
int square (int); // For use with map
bool isEven (int); // For use with filter
int product (int,int); // For use with reduce
bool testPassed();
int main ( ) {
/* Use the main method for your own testing, debugging, etc */
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started