feat: add Array.repeat

This commit is contained in:
Shunsuke Shibayama 2024-02-12 01:30:42 +09:00
parent b9cb197a6f
commit 48cdd2462d
4 changed files with 27 additions and 1 deletions

View file

@ -1603,7 +1603,7 @@ impl Context {
2, 2,
); );
array_add.register_builtin_erg_impl(OP_ADD, t, Immutable, Visibility::BUILTIN_PUBLIC); array_add.register_builtin_erg_impl(OP_ADD, t, Immutable, Visibility::BUILTIN_PUBLIC);
let out_t = array_t(T.clone(), N.clone() + M); let out_t = array_t(T.clone(), N.clone() + M.clone());
array_add.register_builtin_const( array_add.register_builtin_const(
OUTPUT, OUTPUT,
Visibility::BUILTIN_PUBLIC, Visibility::BUILTIN_PUBLIC,
@ -1619,6 +1619,19 @@ impl Context {
) )
.quantify(); .quantify();
array_.register_builtin_erg_impl(FUNC_PUSH, t, Immutable, Visibility::BUILTIN_PUBLIC); array_.register_builtin_erg_impl(FUNC_PUSH, t, Immutable, Visibility::BUILTIN_PUBLIC);
let repeat_t = no_var_fn_met(
arr_t.clone(),
vec![pos(singleton(Nat, M.clone()))],
vec![],
array_t(T.clone(), N.clone() * M.clone()),
)
.quantify();
array_.register_builtin_erg_impl(
FUNC_REPEAT,
repeat_t,
Immutable,
Visibility::BUILTIN_PUBLIC,
);
// [T; N].MutType! = [T; !N] (neither [T!; N] nor [T; N]!) // [T; N].MutType! = [T; !N] (neither [T!; N] nor [T; N]!)
let mut_type = let mut_type =
ValueObj::builtin_class(poly(MUT_ARRAY, vec![TyParam::t(T.clone()), N.clone()])); ValueObj::builtin_class(poly(MUT_ARRAY, vec![TyParam::t(T.clone()), N.clone()]));

View file

@ -243,6 +243,7 @@ const FUNC_DEDUP: &str = "dedup";
const FUNC_CONCAT: &str = "concat"; const FUNC_CONCAT: &str = "concat";
const FUNC_DIFF: &str = "diff"; const FUNC_DIFF: &str = "diff";
const FUNC_PUSH: &str = "push"; const FUNC_PUSH: &str = "push";
const FUNC_REPEAT: &str = "repeat";
const PROC_PUSH: &str = "push!"; const PROC_PUSH: &str = "push!";
const FUNC_MERGE: &str = "merge"; const FUNC_MERGE: &str = "merge";
const PROC_MERGE: &str = "merge!"; const PROC_MERGE: &str = "merge!";

View file

@ -127,6 +127,13 @@ class Array(list):
self.remove(item) self.remove(item)
return self return self
def repeat(self, n):
from copy import deepcopy
new = []
for _ in range(n):
new.extend(deepcopy(self))
return Array(new)
class UnsizedArray: class UnsizedArray:
elem: object elem: object
def __init__(self, elem): def __init__(self, elem):

View file

@ -33,3 +33,8 @@ assert l == [4, 3]
l.clear!() l.clear!()
assert l == [] assert l == []
l2 = [![1]].repeat 3
l2[0].push! 2
ans: Array(Array(Nat)) = [[1, 2], [1], [1]]
assert l2 == ans