# Thread: Beginners Programming Challenge #20

1. ## Re: Beginners Programming Challenge #20

Huh, I should probably pick a winner. I'll wait for mo.reina to finish, though.

all done.

3. ## Re: Beginners Programming Challenge #20

Alright! Sorry it's taken so long to pick a winner for this, school tends to be distracting.

Anyways, the winning entry belong to jwbrase! His Haskell entry was concise, functional (in both senses of the word), and implemented the other functions in terms of reduce. Thank you to all of the participants, and congratulations jwbrase! We'll be looking out for the next challenge.

4. ## Re: Beginners Programming Challenge #20

jwbrase has let me know he can't run the next challenge at the moment - is there anyone else interested in hosting it?

5. ## Re: Beginners Programming Challenge #20

Originally Posted by schauerlich
jwbrase has let me know he can't run the next challenge at the moment - is there anyone else interested in hosting it?
I'd be happy to do it

6. ## Re: Beginners Programming Challenge #20

Originally Posted by Queue29
I'd be happy to do it
Alright. Unless someone from the beginner's team objects, go ahead and make the next one!

7. ## Re: Beginners Programming Challenge #20

I know the challenge is officially over, but i saw noone gave a C++ solution, so i thought i should give it a try (and refresh my template skills which i haven't used for a long time).

Code:
```#include <iostream>
#include <vector>

template <typename Function, typename T>
std::vector<T> map(Function f, std::vector<T> const &data)
{
std::vector<T> result;
for (typename std::vector<T>::const_iterator it = data.begin();
it != data.end();
++it)
result.push_back(f(*it));
return result;
}

template <typename T>
class Square
{
public:
T operator()(T const &x)
{
return x * x;
}
};

template <typename Function, typename T>
std::vector<T> filter(Function f, std::vector<T> const &data)
{
std::vector<T> result;
for (typename std::vector<T>::const_iterator it = data.begin();
it != data.end();
++it)
if (f(*it))
result.push_back(*it);
return result;
}

class isOdd
{
public:
int operator()(int x)
{
return x & 1;
}
};

template <typename Function, typename T>
T reduce(Function f, std::vector<T> const & data, T const &i)
{
T value = i;
for (typename std::vector<T>::const_iterator it = data.begin();
it != data.end();
++it)
value = f(value, *it);
return value;
}

template <typename T>
{
public:
T operator()(T const &v1, T const &v2)
{
return v1 + v2;
}
};

template <typename T>
void print(std::vector<T> const &data)
{
for (typename std::vector<T>::const_iterator it = data.begin();
it != data.end();
++it)
std::cout << *it << ' ';
std::cout << '\n';
}

int main()
{
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> vec(array, array + 10);

std::vector<int> squares = map(Square<int>(), vec);
print(squares);

std::vector<int> odds = filter(isOdd(), vec);
print(odds);

std::vector<int> vec2(array, array + 5);
int sum = reduce(add<int>(), vec2, 0);
std::cout << sum << std::endl;
return 0;
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•