feat(util/list_fn): generalize map_filter template
This commit is contained in:
parent
0651496bf6
commit
86410d392b
3 changed files with 10 additions and 10 deletions
|
@ -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) {
|
||||
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();
|
||||
optional<goal> new_goal = f(in);
|
||||
if (new_goal) {
|
||||
|
|
|
@ -163,7 +163,7 @@ static void tst12() {
|
|||
|
||||
static void tst13() {
|
||||
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) {
|
||||
out = in + 10;
|
||||
return true;
|
||||
|
@ -174,8 +174,8 @@ static void tst13() {
|
|||
std::cout << l2 << "\n";
|
||||
list<int> l3({12, 14, 16});
|
||||
lean_assert_eq(l2, l3);
|
||||
lean_assert(empty(map_filter(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>(l, [](int, int &) { return false; })));
|
||||
lean_assert(empty(map_filter<int>(list<int>(), [](int in, int & out) { out = in; return true; })));
|
||||
}
|
||||
|
||||
static void tst14() {
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
template<typename T, typename F>
|
||||
list<T> map_filter(list<T> const & l, F && f) {
|
||||
template<typename To, typename From, typename F>
|
||||
list<To> map_filter(list<From> const & l, F && f) {
|
||||
if (is_nil(l)) {
|
||||
return l;
|
||||
return list<To>();
|
||||
} else {
|
||||
buffer<typename list<T>::cell*> tmp;
|
||||
buffer<typename list<From>::cell*> tmp;
|
||||
to_buffer(l, tmp);
|
||||
unsigned i = tmp.size();
|
||||
list<T> r;
|
||||
list<To> r;
|
||||
while (i > 0) {
|
||||
--i;
|
||||
T out;
|
||||
To out;
|
||||
if (f(tmp[i]->head(), out))
|
||||
r = cons(out, r);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue