mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-28 04:09:05 +00:00
feat: add Array.update_nth!
This commit is contained in:
parent
07e37c878c
commit
74bca70017
7 changed files with 87 additions and 15 deletions
|
@ -2423,7 +2423,7 @@ impl Context {
|
||||||
NoneType,
|
NoneType,
|
||||||
)
|
)
|
||||||
.quantify();
|
.quantify();
|
||||||
array_mut_.register_py_builtin(PROC_PUSH, t, Some(FUNC_APPEND), 14);
|
array_mut_.register_py_builtin(PROC_PUSH, t, Some(FUNC_APPEND), 15);
|
||||||
let t_extend = pr_met(
|
let t_extend = pr_met(
|
||||||
ref_mut(
|
ref_mut(
|
||||||
array_mut_t.clone(),
|
array_mut_t.clone(),
|
||||||
|
@ -2438,7 +2438,7 @@ impl Context {
|
||||||
NoneType,
|
NoneType,
|
||||||
)
|
)
|
||||||
.quantify();
|
.quantify();
|
||||||
array_mut_.register_py_builtin(PROC_EXTEND, t_extend, Some(FUNC_EXTEND), 23);
|
array_mut_.register_py_builtin(PROC_EXTEND, t_extend, Some(FUNC_EXTEND), 24);
|
||||||
let t_insert = pr_met(
|
let t_insert = pr_met(
|
||||||
ref_mut(
|
ref_mut(
|
||||||
array_mut_t.clone(),
|
array_mut_t.clone(),
|
||||||
|
@ -2453,7 +2453,7 @@ impl Context {
|
||||||
NoneType,
|
NoneType,
|
||||||
)
|
)
|
||||||
.quantify();
|
.quantify();
|
||||||
array_mut_.register_py_builtin(PROC_INSERT, t_insert, Some(FUNC_INSERT), 32);
|
array_mut_.register_py_builtin(PROC_INSERT, t_insert, Some(FUNC_INSERT), 33);
|
||||||
let t_remove = pr_met(
|
let t_remove = pr_met(
|
||||||
ref_mut(
|
ref_mut(
|
||||||
array_mut_t.clone(),
|
array_mut_t.clone(),
|
||||||
|
@ -2468,7 +2468,7 @@ impl Context {
|
||||||
NoneType,
|
NoneType,
|
||||||
)
|
)
|
||||||
.quantify();
|
.quantify();
|
||||||
array_mut_.register_py_builtin(PROC_REMOVE, t_remove, Some(FUNC_REMOVE), 41);
|
array_mut_.register_py_builtin(PROC_REMOVE, t_remove, Some(FUNC_REMOVE), 42);
|
||||||
let t_pop = pr_met(
|
let t_pop = pr_met(
|
||||||
ref_mut(
|
ref_mut(
|
||||||
array_mut_t.clone(),
|
array_mut_t.clone(),
|
||||||
|
@ -2483,7 +2483,7 @@ impl Context {
|
||||||
T.clone(),
|
T.clone(),
|
||||||
)
|
)
|
||||||
.quantify();
|
.quantify();
|
||||||
array_mut_.register_py_builtin(PROC_POP, t_pop, Some(FUNC_POP), 51);
|
array_mut_.register_py_builtin(PROC_POP, t_pop, Some(FUNC_POP), 52);
|
||||||
let t_clear = pr0_met(
|
let t_clear = pr0_met(
|
||||||
ref_mut(
|
ref_mut(
|
||||||
array_mut_t.clone(),
|
array_mut_t.clone(),
|
||||||
|
@ -2492,7 +2492,7 @@ impl Context {
|
||||||
NoneType,
|
NoneType,
|
||||||
)
|
)
|
||||||
.quantify();
|
.quantify();
|
||||||
array_mut_.register_py_builtin(PROC_CLEAR, t_clear, Some(FUNC_CLEAR), 60);
|
array_mut_.register_py_builtin(PROC_CLEAR, t_clear, Some(FUNC_CLEAR), 61);
|
||||||
let t_sort = pr_met(
|
let t_sort = pr_met(
|
||||||
ref_mut(array_mut_t.clone(), None),
|
ref_mut(array_mut_t.clone(), None),
|
||||||
vec![],
|
vec![],
|
||||||
|
@ -2504,9 +2504,9 @@ impl Context {
|
||||||
NoneType,
|
NoneType,
|
||||||
)
|
)
|
||||||
.quantify();
|
.quantify();
|
||||||
array_mut_.register_py_builtin(PROC_SORT, t_sort, Some(FUNC_SORT), 77);
|
array_mut_.register_py_builtin(PROC_SORT, t_sort, Some(FUNC_SORT), 78);
|
||||||
let t_reverse = pr0_met(ref_mut(array_mut_t.clone(), None), NoneType).quantify();
|
let t_reverse = pr0_met(ref_mut(array_mut_t.clone(), None), NoneType).quantify();
|
||||||
array_mut_.register_py_builtin(PROC_REVERSE, t_reverse, Some(FUNC_REVERSE), 86);
|
array_mut_.register_py_builtin(PROC_REVERSE, t_reverse, Some(FUNC_REVERSE), 87);
|
||||||
let t = pr_met(
|
let t = pr_met(
|
||||||
array_mut_t.clone(),
|
array_mut_t.clone(),
|
||||||
vec![kw(KW_FUNC, nd_func(vec![anon(T.clone())], None, T.clone()))],
|
vec![kw(KW_FUNC, nd_func(vec![anon(T.clone())], None, T.clone()))],
|
||||||
|
@ -2515,7 +2515,16 @@ impl Context {
|
||||||
NoneType,
|
NoneType,
|
||||||
)
|
)
|
||||||
.quantify();
|
.quantify();
|
||||||
array_mut_.register_py_builtin(PROC_STRICT_MAP, t, None, 95);
|
array_mut_.register_py_builtin(PROC_STRICT_MAP, t, None, 96);
|
||||||
|
let t_update_nth = pr_met(
|
||||||
|
ref_mut(array_mut_t.clone(), None),
|
||||||
|
vec![kw(KW_IDX, Nat), kw(KW_FUNC, func1(T.clone(), T.clone()))],
|
||||||
|
None,
|
||||||
|
vec![],
|
||||||
|
NoneType,
|
||||||
|
)
|
||||||
|
.quantify();
|
||||||
|
array_mut_.register_py_builtin(PROC_UPDATE_NTH, t_update_nth, Some(FUNC_UPDATE_NTH), 105);
|
||||||
let f_t = kw(
|
let f_t = kw(
|
||||||
KW_FUNC,
|
KW_FUNC,
|
||||||
func(vec![kw(KW_OLD, arr_t.clone())], None, vec![], arr_t.clone()),
|
func(vec![kw(KW_OLD, arr_t.clone())], None, vec![], arr_t.clone()),
|
||||||
|
|
|
@ -228,6 +228,8 @@ const PY_MODULE: &str = "PyModule";
|
||||||
const GENERIC_ARRAY: &str = "GenericArray";
|
const GENERIC_ARRAY: &str = "GenericArray";
|
||||||
const ARRAY: &str = "Array";
|
const ARRAY: &str = "Array";
|
||||||
const MUT_ARRAY: &str = "Array!";
|
const MUT_ARRAY: &str = "Array!";
|
||||||
|
const FUNC_UPDATE_NTH: &str = "update_nth";
|
||||||
|
const PROC_UPDATE_NTH: &str = "update_nth!";
|
||||||
const FUNC_PARTITION: &str = "partition";
|
const FUNC_PARTITION: &str = "partition";
|
||||||
const FUNC_DEDUP: &str = "dedup";
|
const FUNC_DEDUP: &str = "dedup";
|
||||||
const FUNC_CONCAT: &str = "concat";
|
const FUNC_CONCAT: &str = "concat";
|
||||||
|
@ -427,6 +429,8 @@ const FUNDAMENTAL_LEN: &str = "__len__";
|
||||||
const FUNDAMENTAL_CONTAINS: &str = "__contains__";
|
const FUNDAMENTAL_CONTAINS: &str = "__contains__";
|
||||||
const FUNDAMENTAL_CALL: &str = "__call__";
|
const FUNDAMENTAL_CALL: &str = "__call__";
|
||||||
const FUNDAMENTAL_NAME: &str = "__name__";
|
const FUNDAMENTAL_NAME: &str = "__name__";
|
||||||
|
const FUNDAMENTAL_FILE: &str = "__file__";
|
||||||
|
const FUNDAMENTAL_PACKAGE: &str = "__package__";
|
||||||
const FUNDAMENTAL_STR: &str = "__str__";
|
const FUNDAMENTAL_STR: &str = "__str__";
|
||||||
const FUNDAMENTAL_HASH: &str = "__hash__";
|
const FUNDAMENTAL_HASH: &str = "__hash__";
|
||||||
const FUNDAMENTAL_INT: &str = "__int__";
|
const FUNDAMENTAL_INT: &str = "__int__";
|
||||||
|
@ -1075,6 +1079,20 @@ impl Context {
|
||||||
vis.clone(),
|
vis.clone(),
|
||||||
Some(FUNDAMENTAL_NAME),
|
Some(FUNDAMENTAL_NAME),
|
||||||
);
|
);
|
||||||
|
self.register_builtin_py_impl(
|
||||||
|
FUNDAMENTAL_FILE,
|
||||||
|
Str,
|
||||||
|
Immutable,
|
||||||
|
vis.clone(),
|
||||||
|
Some(FUNDAMENTAL_FILE),
|
||||||
|
);
|
||||||
|
self.register_builtin_py_impl(
|
||||||
|
FUNDAMENTAL_PACKAGE,
|
||||||
|
Str | NoneType,
|
||||||
|
Immutable,
|
||||||
|
vis.clone(),
|
||||||
|
Some(FUNDAMENTAL_PACKAGE),
|
||||||
|
);
|
||||||
if ERG_MODE {
|
if ERG_MODE {
|
||||||
self.register_builtin_py_impl(
|
self.register_builtin_py_impl(
|
||||||
FUNC_MODULE,
|
FUNC_MODULE,
|
||||||
|
|
|
@ -94,3 +94,12 @@ array = pyimport "Array"
|
||||||
assert arr == [3, 4]
|
assert arr == [3, 4]
|
||||||
'''
|
'''
|
||||||
strict_map!: |T, N: Nat|(self: Array!(T, N), f: T -> T) => NoneType
|
strict_map!: |T, N: Nat|(self: Array!(T, N), f: T -> T) => NoneType
|
||||||
|
'''
|
||||||
|
Update `index`-th element of the array according to the passed function `f`.
|
||||||
|
'''
|
||||||
|
'''erg
|
||||||
|
arr = ![1, 2]
|
||||||
|
arr.udpate_nth! 0, x -> x + 1
|
||||||
|
assert arr == [2, 2]
|
||||||
|
'''
|
||||||
|
update_nth!: |T, N: Nat|(self: Array!(T, N), index: Nat, f: T -> T) => NoneType
|
||||||
|
|
|
@ -69,3 +69,6 @@ class Array(list):
|
||||||
if not contains_operator(elem_t, elem):
|
if not contains_operator(elem_t, elem):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def update_nth(self, index, f):
|
||||||
|
self[index] = f(self[index])
|
||||||
|
|
|
@ -3321,6 +3321,13 @@ impl Identifier {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn public_with_loc(dot: Token, name: Str, loc: Location) -> Self {
|
||||||
|
Self::new(
|
||||||
|
VisModifierSpec::Public(dot),
|
||||||
|
VarName::from_str_and_loc(name, loc),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn public_from_token(dot: Token, symbol: Token) -> Self {
|
pub fn public_from_token(dot: Token, symbol: Token) -> Self {
|
||||||
Self::new(VisModifierSpec::Public(dot), VarName::new(symbol))
|
Self::new(VisModifierSpec::Public(dot), VarName::new(symbol))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1465,14 +1465,14 @@ impl Desugarer {
|
||||||
match acc {
|
match acc {
|
||||||
// x[y] => x.__getitem__(y)
|
// x[y] => x.__getitem__(y)
|
||||||
Accessor::Subscr(subscr) => {
|
Accessor::Subscr(subscr) => {
|
||||||
|
let loc = subscr.loc();
|
||||||
let args = Args::single(PosArg::new(Self::rec_desugar_acc(*subscr.index)));
|
let args = Args::single(PosArg::new(Self::rec_desugar_acc(*subscr.index)));
|
||||||
let line = subscr.obj.ln_begin().unwrap_or(1);
|
|
||||||
let call = Call::new(
|
let call = Call::new(
|
||||||
Self::rec_desugar_acc(*subscr.obj),
|
Self::rec_desugar_acc(*subscr.obj),
|
||||||
Some(Identifier::public_with_line(
|
Some(Identifier::public_with_loc(
|
||||||
DOT,
|
DOT,
|
||||||
Str::ever("__getitem__"),
|
Str::ever("__getitem__"),
|
||||||
line,
|
loc,
|
||||||
)),
|
)),
|
||||||
args,
|
args,
|
||||||
);
|
);
|
||||||
|
@ -1480,14 +1480,14 @@ impl Desugarer {
|
||||||
}
|
}
|
||||||
// x.0 => x.__Tuple_getitem__(0)
|
// x.0 => x.__Tuple_getitem__(0)
|
||||||
Accessor::TupleAttr(tattr) => {
|
Accessor::TupleAttr(tattr) => {
|
||||||
|
let loc = tattr.loc();
|
||||||
let args = Args::single(PosArg::new(Expr::Literal(tattr.index)));
|
let args = Args::single(PosArg::new(Expr::Literal(tattr.index)));
|
||||||
let line = tattr.obj.ln_begin().unwrap_or(1);
|
|
||||||
let call = Call::new(
|
let call = Call::new(
|
||||||
Self::rec_desugar_acc(*tattr.obj),
|
Self::rec_desugar_acc(*tattr.obj),
|
||||||
Some(Identifier::public_with_line(
|
Some(Identifier::public_with_loc(
|
||||||
DOT,
|
DOT,
|
||||||
Str::ever("__Tuple_getitem__"),
|
Str::ever("__Tuple_getitem__"),
|
||||||
line,
|
loc,
|
||||||
)),
|
)),
|
||||||
args,
|
args,
|
||||||
);
|
);
|
||||||
|
|
|
@ -7,3 +7,29 @@ assert [1, 1, 2].dedup() == [1, 2]
|
||||||
assert [0.0, 0.1, 10.0, 20.0, 20.1].dedup((lhs, rhs) -> abs(lhs - rhs) < 1.0) == [0.1, 10.0, 20.1]
|
assert [0.0, 0.1, 10.0, 20.0, 20.1].dedup((lhs, rhs) -> abs(lhs - rhs) < 1.0) == [0.1, 10.0, 20.1]
|
||||||
|
|
||||||
assert [-2, -1, 0, 1, 2].partition(x -> x >= 0) == ([0, 1, 2], [-2, -1])
|
assert [-2, -1, 0, 1, 2].partition(x -> x >= 0) == ([0, 1, 2], [-2, -1])
|
||||||
|
|
||||||
|
l = ![1]
|
||||||
|
|
||||||
|
l.push! 2
|
||||||
|
assert l == [1, 2]
|
||||||
|
|
||||||
|
l.extend! [3, 4]
|
||||||
|
assert l == [1, 2, 3, 4]
|
||||||
|
|
||||||
|
l.update_nth! 0, (x: Nat) -> x + 2
|
||||||
|
assert l == [3, 2, 3, 4]
|
||||||
|
|
||||||
|
l.sort!()
|
||||||
|
assert l == [2, 3, 3, 4]
|
||||||
|
|
||||||
|
discard l.pop! 0
|
||||||
|
assert l == [3, 3, 4]
|
||||||
|
|
||||||
|
l.remove! 3
|
||||||
|
assert l == [3, 4]
|
||||||
|
|
||||||
|
l.reverse!()
|
||||||
|
assert l == [4, 3]
|
||||||
|
|
||||||
|
l.clear!()
|
||||||
|
assert l == []
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue