feat: add [x; _] literal

This commit is contained in:
Shunsuke Shibayama 2023-10-02 20:49:21 +09:00
parent 828441f9f9
commit f1d71e0dab
18 changed files with 165 additions and 28 deletions

View file

@ -3,6 +3,8 @@ from _erg_range import Range
from _erg_nat import NatMut
from _erg_int import IntMut
from _erg_contains_operator import contains_operator
from _erg_result import is_ok
from _erg_result import Error
class Array(list):
def try_new(arr): # -> Result[Array]
@ -11,6 +13,21 @@ class Array(list):
else:
return Error("not a list")
def generic_try_new(arr, cls = None): # -> Result[Array]
if cls is None:
return Array.try_new(arr)
else:
elem_t = cls.__args__[0]
elems = []
for elem in arr:
# TODO: nested check
elem = elem_t.try_new(elem)
if is_ok(elem):
elems.append(elem)
else:
return Error("not a " + str(elem_t))
return Array(elems)
def dedup(self, same_bucket=None):
if same_bucket is None:
return Array(list(set(self)))
@ -77,3 +94,8 @@ class Array(list):
def update_nth(self, index, f):
self[index] = f(self[index])
class UnsizedArray:
elem: object
def __init__(self, elem):
self.elem = elem

View file

@ -12,6 +12,8 @@ def contains_operator(y, elem) -> bool:
elif is_type(y):
if _isinstance(elem, y):
return True
elif hasattr(y, "generic_try_new"):
return is_ok(y.generic_try_new(elem, y))
elif hasattr(y, "try_new") and is_ok(y.try_new(elem)):
return True
elif hasattr(y, "__origin__") and hasattr(y.__origin__, "type_check"):

View file

@ -13,7 +13,7 @@ from _erg_nat import Nat, NatMut
from _erg_bool import Bool
from _erg_bytes import Bytes
from _erg_str import Str, StrMut
from _erg_array import Array
from _erg_array import Array, UnsizedArray
from _erg_dict import Dict
from _erg_set import Set
from _erg_contains_operator import contains_operator