|
Boost : |
From: Powell, Gary (powellg_at_[hidden])
Date: 2002-03-29 16:04:07
Asger>>May I congratulate Jaakko and Gary for the inclusion of LL in
Boost.
Thanks!
Asger>>----------------------------
Problem 1: Implement map, which applies a function to each element of a
container, and returns a new container with the resulting values.
Then, it is arguably better to use transform:
<<------------------------------
template<typename X>
X map(X const & x, boost::function f) {
X result(x.size());
transform(x.begin(), x.end(), back_inserter(result), f);
^^^^^^^^^^^^^
return result;
}
Unquestionably, its better. Just be sure to use back_inserter()
Asger>>-----------------------------
Problem 2: From a container x, create the container with each element
squared.
<<-----------------------------------
template<typename X>
X square_container(X const & x) {
X result(x.size());
transform(x.begin(), x.end(), back_inserter(result), _1 * _1 );
^^^^^^^^
return result;
}
Just use LL where its appropriate.
Asger>>-------------------------------
It would be nicer if LL worked together with boost::function.
<<-----------------------------------
It's on the list to TODO.
Asger>>------------------------
Problem 3: Implement concat, which joins a container of containers into
a single container.
<<-----------------------------
template<typename L>
typename L::value_type
concat(L const & l1, L const & l2) {
typename L::value_type result;
// just nest the loops.
for_each(l1.begin(), l2.end(),
ll::transform( (&_1)->*begin(),
(&_1)->*end(),
back_inserter(result),
_1)
);
return result;
}
/note: since we can't overload operator.(), we use operator& to allow us
to use
Operator ->* instead of bind. (which also works.)
Like any good programmer/carpenter/plumber, we expect you to use the
right tool for the job. With LL you just get some more tools.
Yours,
-gary-
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk