feat: add {Str, List}.from

This commit is contained in:
Shunsuke Shibayama 2024-04-30 18:44:31 +09:00
parent 65d05cb37b
commit 96f4c1cf98
10 changed files with 56 additions and 5 deletions

View file

@ -1247,6 +1247,13 @@ impl Context {
Visibility::BUILTIN_PUBLIC,
Some(FUNC_ISPRINTABLE),
);
str_.register_builtin_py_impl(
FUNC_FROM,
no_var_fn_met(Str, vec![kw(KW_NTH, Nat)], vec![], Str),
Immutable,
Visibility::BUILTIN_PUBLIC,
Some(FUNC_FROM_),
);
let str_getitem_t = fn1_kw_met(Str, kw(KW_IDX, Nat | poly(RANGE, vec![ty_tp(Int)])), Str);
str_.register_builtin_erg_impl(
FUNDAMENTAL_GETITEM,
@ -1796,6 +1803,20 @@ impl Context {
Some(list_remove_all_t),
list_remove_all,
);
let list_from = no_var_fn_met(
unknown_len_list_t(T.clone()),
vec![kw(KW_NTH, Nat)],
vec![],
unknown_len_list_t(T.clone()),
)
.quantify();
list_.register_builtin_py_impl(
FUNC_FROM,
list_from,
Immutable,
Visibility::BUILTIN_PUBLIC,
Some(FUNC_FROM_),
);
list_
.register_trait(self, poly(INDEXABLE, vec![ty_tp(input), ty_tp(T.clone())]))
.unwrap();

View file

@ -332,6 +332,8 @@ const FUNC_REMOVE: &str = "remove";
const PROC_REMOVE: &str = "remove!";
const FUNC_REMOVE_AT: &str = "remove_at";
const FUNC_REMOVE_ALL: &str = "remove_all";
const FUNC_FROM: &str = "from";
const FUNC_FROM_: &str = "from_";
const FUNC_POP: &str = "pop";
const PROC_POP: &str = "pop!";
const FUNC_CLEAR: &str = "clear";
@ -681,6 +683,7 @@ const KW_FILLCHAR: &str = "fillchar";
const KW_WIDTH: &str = "width";
const KW_ARGS: &str = "args";
const KW_KWARGS: &str = "kwargs";
const KW_NTH: &str = "nth";
const KW_IDX: &str = "idx";
const KW_LHS: &str = "lhs";
const KW_RHS: &str = "rhs";