feat(util/list_fn): generalize map_filter template

This commit is contained in:
Leonardo de Moura 2014-10-08 18:04:18 -07:00
parent 0651496bf6
commit 86410d392b
3 changed files with 10 additions and 10 deletions

View file

@ -49,7 +49,7 @@ format proof_state::pp(formatter const & fmt) const {
} }
goals map_goals(proof_state const & s, std::function<optional<goal>(goal const & g)> f) { goals map_goals(proof_state const & s, std::function<optional<goal>(goal const & g)> f) {
return map_filter(s.get_goals(), [=](goal const & in, goal & out) -> bool { return map_filter<goal>(s.get_goals(), [=](goal const & in, goal & out) -> bool {
check_interrupted(); check_interrupted();
optional<goal> new_goal = f(in); optional<goal> new_goal = f(in);
if (new_goal) { if (new_goal) {

View file

@ -163,7 +163,7 @@ static void tst12() {
static void tst13() { static void tst13() {
list<int> l({1, 2, 3, 4, 5, 6, 7}); list<int> l({1, 2, 3, 4, 5, 6, 7});
list<int> l2 = map_filter(l, [](int in, int & out) { list<int> l2 = map_filter<int>(l, [](int in, int & out) {
if (in % 2 == 0) { if (in % 2 == 0) {
out = in + 10; out = in + 10;
return true; return true;
@ -174,8 +174,8 @@ static void tst13() {
std::cout << l2 << "\n"; std::cout << l2 << "\n";
list<int> l3({12, 14, 16}); list<int> l3({12, 14, 16});
lean_assert_eq(l2, l3); lean_assert_eq(l2, l3);
lean_assert(empty(map_filter(l, [](int, int &) { return false; }))); lean_assert(empty(map_filter<int>(l, [](int, int &) { return false; })));
lean_assert(empty(map_filter(list<int>(), [](int in, int & out) { out = in; return true; }))); lean_assert(empty(map_filter<int>(list<int>(), [](int in, int & out) { out = in; return true; })));
} }
static void tst14() { static void tst14() {

View file

@ -206,18 +206,18 @@ list<T> remove_last(list<T> const & l, P && p) {
If \c out becomes part of the result iff \c f returns true. If \c out becomes part of the result iff \c f returns true.
*/ */
template<typename T, typename F> template<typename To, typename From, typename F>
list<T> map_filter(list<T> const & l, F && f) { list<To> map_filter(list<From> const & l, F && f) {
if (is_nil(l)) { if (is_nil(l)) {
return l; return list<To>();
} else { } else {
buffer<typename list<T>::cell*> tmp; buffer<typename list<From>::cell*> tmp;
to_buffer(l, tmp); to_buffer(l, tmp);
unsigned i = tmp.size(); unsigned i = tmp.size();
list<T> r; list<To> r;
while (i > 0) { while (i > 0) {
--i; --i;
T out; To out;
if (f(tmp[i]->head(), out)) if (f(tmp[i]->head(), out))
r = cons(out, r); r = cons(out, r);
} }