Skip to content

Commit fca456b

Browse files
committed
added filter()
1 parent 39b174e commit fca456b

6 files changed

Lines changed: 61 additions & 0 deletions

File tree

py/mpqstrraw.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Q(complex)
4242
Q(dict)
4343
Q(divmod)
4444
Q(enumerate)
45+
Q(filter)
4546
Q(float)
4647
Q(hash)
4748
Q(int)

py/obj.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ extern const mp_obj_type_t map_type;
300300
// enumerate
301301
extern const mp_obj_type_t enumerate_type;
302302

303+
// filter
304+
extern const mp_obj_type_t filter_type;
305+
303306
// dict
304307
extern const mp_obj_type_t dict_type;
305308
uint mp_obj_dict_len(mp_obj_t self_in);

py/objfilter.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <stdlib.h>
2+
#include <assert.h>
3+
4+
#include "misc.h"
5+
#include "mpconfig.h"
6+
#include "obj.h"
7+
#include "runtime.h"
8+
9+
typedef struct _mp_obj_filter_t {
10+
mp_obj_base_t base;
11+
mp_obj_t fun;
12+
mp_obj_t iter;
13+
} mp_obj_filter_t;
14+
15+
static mp_obj_t filter_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
16+
/* NOTE: args are backwards */
17+
mp_obj_filter_t *o = m_new_obj(mp_obj_filter_t);
18+
assert(n_args == 2);
19+
o->base.type = &filter_type;
20+
o->fun = args[1];
21+
o->iter = rt_getiter(args[0]);
22+
return o;
23+
}
24+
25+
static mp_obj_t filter_getiter(mp_obj_t self_in) {
26+
return self_in;
27+
}
28+
29+
static mp_obj_t filter_iternext(mp_obj_t self_in) {
30+
assert(MP_OBJ_IS_TYPE(self_in, &filter_type));
31+
mp_obj_filter_t *self = self_in;
32+
mp_obj_t next;
33+
while ((next = rt_iternext(self->iter)) != mp_const_stop_iteration) {
34+
mp_obj_t val;
35+
if (self->fun != mp_const_none) {
36+
val = rt_call_function_n(self->fun, 1, &next);
37+
} else {
38+
val = next;
39+
}
40+
if (rt_is_true(val)) {
41+
return next;
42+
}
43+
}
44+
return mp_const_stop_iteration;
45+
}
46+
47+
const mp_obj_type_t filter_type = {
48+
{ &mp_const_type },
49+
"filter",
50+
.make_new = filter_make_new,
51+
.getiter = filter_getiter,
52+
.iternext = filter_iternext,
53+
};

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ PY_O_BASENAME = \
7979
objdict.o \
8080
objenumerate.o \
8181
objexcept.o \
82+
objfilter.o \
8283
objfloat.o \
8384
objfun.o \
8485
objgenerator.o \

py/runtime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ void rt_init(void) {
106106
#endif
107107
mp_map_add_qstr(&map_builtins, MP_QSTR_dict, (mp_obj_t)&dict_type);
108108
mp_map_add_qstr(&map_builtins, MP_QSTR_enumerate, (mp_obj_t)&enumerate_type);
109+
mp_map_add_qstr(&map_builtins, MP_QSTR_filter, (mp_obj_t)&filter_type);
109110
#if MICROPY_ENABLE_FLOAT
110111
mp_map_add_qstr(&map_builtins, MP_QSTR_float, (mp_obj_t)&float_type);
111112
#endif

tests/basics/tests/filter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
print(list(filter(lambda x: x & 1, range(-3, 4))))
2+
print(list(filter(None, range(-3, 4))))

0 commit comments

Comments
 (0)