mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-27 19:59:07 +00:00
feat: add Array.repeat
This commit is contained in:
parent
b9cb197a6f
commit
48cdd2462d
4 changed files with 27 additions and 1 deletions
|
@ -1603,7 +1603,7 @@ impl Context {
|
|||
2,
|
||||
);
|
||||
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(
|
||||
OUTPUT,
|
||||
Visibility::BUILTIN_PUBLIC,
|
||||
|
@ -1619,6 +1619,19 @@ impl Context {
|
|||
)
|
||||
.quantify();
|
||||
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]!)
|
||||
let mut_type =
|
||||
ValueObj::builtin_class(poly(MUT_ARRAY, vec![TyParam::t(T.clone()), N.clone()]));
|
||||
|
|
|
@ -243,6 +243,7 @@ const FUNC_DEDUP: &str = "dedup";
|
|||
const FUNC_CONCAT: &str = "concat";
|
||||
const FUNC_DIFF: &str = "diff";
|
||||
const FUNC_PUSH: &str = "push";
|
||||
const FUNC_REPEAT: &str = "repeat";
|
||||
const PROC_PUSH: &str = "push!";
|
||||
const FUNC_MERGE: &str = "merge";
|
||||
const PROC_MERGE: &str = "merge!";
|
||||
|
|
|
@ -127,6 +127,13 @@ class Array(list):
|
|||
self.remove(item)
|
||||
return self
|
||||
|
||||
def repeat(self, n):
|
||||
from copy import deepcopy
|
||||
new = []
|
||||
for _ in range(n):
|
||||
new.extend(deepcopy(self))
|
||||
return Array(new)
|
||||
|
||||
class UnsizedArray:
|
||||
elem: object
|
||||
def __init__(self, elem):
|
||||
|
|
|
@ -33,3 +33,8 @@ assert l == [4, 3]
|
|||
|
||||
l.clear!()
|
||||
assert l == []
|
||||
|
||||
l2 = [![1]].repeat 3
|
||||
l2[0].push! 2
|
||||
ans: Array(Array(Nat)) = [[1, 2], [1], [1]]
|
||||
assert l2 == ans
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue