Cleaned up sorted() as per Damien's suggestions.

This commit is contained in:
John R. Lenton 2014-01-13 19:52:28 +00:00
parent 5c76839559
commit 2ded68db77
2 changed files with 2 additions and 8 deletions

View File

@ -297,7 +297,6 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
return value; return value;
} }
extern mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs);
static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) { static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) {
mp_obj_t *args_items = NULL; mp_obj_t *args_items = NULL;
uint args_len = 0; uint args_len = 0;
@ -309,13 +308,7 @@ static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) {
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
"must use keyword argument for key function")); "must use keyword argument for key function"));
} }
mp_obj_t iterable = rt_getiter(args_items[0]); mp_obj_t self = list_type.make_new((mp_obj_t)&list_type, 1, args_items);
mp_obj_t self = rt_build_list(0, NULL);
mp_obj_t item;
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
rt_list_append(self, item);
}
mp_obj_t new_args = rt_build_tuple(1, &self); mp_obj_t new_args = rt_build_tuple(1, &self);
list_sort(new_args, kwargs); list_sort(new_args, kwargs);

View File

@ -292,6 +292,7 @@ extern const mp_obj_type_t list_type;
mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg); mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items); void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value); void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
mp_obj_t list_sort(mp_obj_t args, struct _mp_map_t *kwargs);
// dict // dict
extern const mp_obj_type_t dict_type; extern const mp_obj_type_t dict_type;