Commit 5c768395 authored by John R. Lenton's avatar John R. Lenton
Browse files

sorted

parent 07205ec3
...@@ -296,3 +296,29 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { ...@@ -296,3 +296,29 @@ 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) {
mp_obj_t *args_items = NULL;
uint args_len = 0;
assert(MP_OBJ_IS_TYPE(args, &tuple_type));
mp_obj_tuple_get(args, &args_len, &args_items);
assert(args_len >= 1);
if (args_len > 1) {
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
"must use keyword argument for key function"));
}
mp_obj_t iterable = rt_getiter(args_items[0]);
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);
list_sort(new_args, kwargs);
return self;
}
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, mp_builtin_sorted);
...@@ -23,3 +23,4 @@ mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args); ...@@ -23,3 +23,4 @@ mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args);
mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args); mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args);
mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args); mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args);
mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args); mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sorted_obj);
...@@ -57,6 +57,7 @@ Q(pow) ...@@ -57,6 +57,7 @@ Q(pow)
Q(print) Q(print)
Q(range) Q(range)
Q(set) Q(set)
Q(sorted)
Q(sum) Q(sum)
Q(tuple) Q(tuple)
Q(type) Q(type)
......
...@@ -248,13 +248,14 @@ static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, bool r ...@@ -248,13 +248,14 @@ static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, bool r
} }
} }
static mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs) { mp_obj_t list_sort(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;
assert(MP_OBJ_IS_TYPE(args, &tuple_type)); assert(MP_OBJ_IS_TYPE(args, &tuple_type));
mp_obj_tuple_get(args, &args_len, &args_items); mp_obj_tuple_get(args, &args_len, &args_items);
assert(args_len >= 1); assert(args_len >= 1);
assert(MP_OBJ_IS_TYPE(args_items[0], &list_type));
if (args_len > 1) { if (args_len > 1) {
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
"list.sort takes no positional arguments")); "list.sort takes no positional arguments"));
......
...@@ -134,6 +134,7 @@ void rt_init(void) { ...@@ -134,6 +134,7 @@ void rt_init(void) {
mp_map_add_qstr(&map_builtins, MP_QSTR_pow, rt_make_function_var(2, mp_builtin_pow)); mp_map_add_qstr(&map_builtins, MP_QSTR_pow, rt_make_function_var(2, mp_builtin_pow));
mp_map_add_qstr(&map_builtins, MP_QSTR_print, rt_make_function_var(0, mp_builtin_print)); mp_map_add_qstr(&map_builtins, MP_QSTR_print, rt_make_function_var(0, mp_builtin_print));
mp_map_add_qstr(&map_builtins, MP_QSTR_range, rt_make_function_var(1, mp_builtin_range)); mp_map_add_qstr(&map_builtins, MP_QSTR_range, rt_make_function_var(1, mp_builtin_range));
mp_map_add_qstr(&map_builtins, MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_sum, rt_make_function_var(1, mp_builtin_sum)); mp_map_add_qstr(&map_builtins, MP_QSTR_sum, rt_make_function_var(1, mp_builtin_sum));
next_unique_code_id = 1; // 0 indicates "no code" next_unique_code_id = 1; // 0 indicates "no code"
......
...@@ -440,10 +440,17 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t ** ...@@ -440,10 +440,17 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **
case MP_BC_CALL_FUNCTION: case MP_BC_CALL_FUNCTION:
DECODE_UINT; DECODE_UINT;
assert((unum & 0xff00) == 0); // n_keyword if ((unum & 0xff00) == 0) {
unum &= 0xff; // n_positional // no keywords
sp += unum; unum &= 0xff; // n_positional
*sp = rt_call_function_n(*sp, unum, sp - unum); sp += unum;
*sp = rt_call_function_n(*sp, unum, sp - unum);
} else {
// keywords
int argsize = (unum & 0xff) + ((unum >> 7) & 0x1fe);
sp += argsize;
*sp = rt_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp - argsize);
}
break; break;
case MP_BC_CALL_METHOD: case MP_BC_CALL_METHOD:
......
print(sorted(set(range(100))))
print(sorted(set(range(100)), key=lambda x: x + 100*(x % 2)))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment