Skip to content

Commit 39b174e

Browse files
committed
Added map
1 parent ff8007c commit 39b174e

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed

py/mpqstrraw.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Q(issubclass)
5050
Q(iter)
5151
Q(len)
5252
Q(list)
53+
Q(map)
5354
Q(max)
5455
Q(min)
5556
Q(next)

py/obj.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
294294
void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
295295
mp_obj_t mp_obj_list_sort(mp_obj_t args, struct _mp_map_t *kwargs);
296296

297+
// map (the python builtin, not the dict implementation detail)
298+
extern const mp_obj_type_t map_type;
299+
297300
// enumerate
298301
extern const mp_obj_type_t enumerate_type;
299302

py/objmap.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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_map_t {
10+
mp_obj_base_t base;
11+
machine_uint_t n_iters;
12+
mp_obj_t fun;
13+
mp_obj_t iters[];
14+
} mp_obj_map_t;
15+
16+
static mp_obj_t map_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
17+
/* NOTE: args are backwards */
18+
mp_obj_map_t *o = m_new_obj_var(mp_obj_map_t, mp_obj_t, n_args - 1);
19+
assert(n_args >= 2);
20+
o->base.type = &map_type;
21+
o->n_iters = n_args - 1;
22+
o->fun = args[n_args - 1];
23+
for (int i = 0; i < n_args - 1; i++) {
24+
o->iters[i] = rt_getiter(args[n_args-i-2]);
25+
}
26+
return o;
27+
}
28+
29+
static mp_obj_t map_getiter(mp_obj_t self_in) {
30+
return self_in;
31+
}
32+
33+
static mp_obj_t map_iternext(mp_obj_t self_in) {
34+
assert(MP_OBJ_IS_TYPE(self_in, &map_type));
35+
mp_obj_map_t *self = self_in;
36+
mp_obj_t *nextses = m_new(mp_obj_t, self->n_iters);
37+
38+
for (int i = 0; i < self->n_iters; i++) {
39+
mp_obj_t next = rt_iternext(self->iters[i]);
40+
if (next == mp_const_stop_iteration) {
41+
m_del(mp_obj_t, nextses, self->n_iters);
42+
return mp_const_stop_iteration;
43+
}
44+
nextses[i] = next;
45+
}
46+
return rt_call_function_n(self->fun, self->n_iters, nextses);
47+
}
48+
49+
const mp_obj_type_t map_type = {
50+
{ &mp_const_type },
51+
"map",
52+
.make_new = map_make_new,
53+
.getiter = map_getiter,
54+
.iternext = map_iternext,
55+
};

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ PY_O_BASENAME = \
8484
objgenerator.o \
8585
objint.o \
8686
objlist.o \
87+
objmap.o \
8788
objmodule.o \
8889
objnone.o \
8990
objrange.o \

py/runtime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ void rt_init(void) {
111111
#endif
112112
mp_map_add_qstr(&map_builtins, MP_QSTR_int, (mp_obj_t)&int_type);
113113
mp_map_add_qstr(&map_builtins, MP_QSTR_list, (mp_obj_t)&list_type);
114+
mp_map_add_qstr(&map_builtins, MP_QSTR_map, (mp_obj_t)&map_type);
114115
mp_map_add_qstr(&map_builtins, MP_QSTR_set, (mp_obj_t)&set_type);
115116
mp_map_add_qstr(&map_builtins, MP_QSTR_tuple, (mp_obj_t)&tuple_type);
116117
mp_map_add_qstr(&map_builtins, MP_QSTR_type, (mp_obj_t)&mp_const_type);

tests/basics/tests/map.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
print(list(map(lambda x: x & 1, range(-3, 4))))
2+
print(list(map(abs, range(-3, 4))))
3+
print(list(map(set, [[i] for i in range(-3, 4)])))
4+
print(list(map(pow, range(4), range(4))))

0 commit comments

Comments
 (0)