fix: in operator

* remove `In` trait, `__in__`, `__notin__` function
* desugar to `__contains__`
* impl Collection for Array, Set, Tuple
This commit is contained in:
Shunsuke Shibayama 2023-08-13 01:01:06 +09:00
parent 4f93c393c0
commit 2e9fb48f2e
19 changed files with 154 additions and 142 deletions

View file

@ -3,7 +3,9 @@ from _erg_range import Range
from collections import namedtuple
def in_operator(elem, y):
# (elem in y) == contains_operator(y, elem)
def contains_operator(y, elem):
# 1 in Int
if type(y) == type:
if isinstance(elem, y):
return True
@ -11,22 +13,27 @@ def in_operator(elem, y):
return True
# TODO: trait check
return False
elif isinstance(y, list) and (
# [1] in [Int]
elif isinstance(y, list) and isinstance(elem, list) and (
type(y[0]) == type or isinstance(y[0], Range)
):
# FIXME:
type_check = in_operator(elem[0], y[0])
type_check = contains_operator(y[0], elem[0])
len_check = len(elem) == len(y)
return type_check and len_check
elif isinstance(y, tuple):
# (1, 2) in (Int, Int)
elif isinstance(y, tuple) and isinstance(elem, tuple) and (
type(y[0]) == type or isinstance(y[0], Range)
):
if not hasattr(elem, "__iter__"):
return False
type_check = all(map(lambda x: in_operator(x[0], x[1]), zip(elem, y)))
type_check = all(map(lambda x: contains_operator(x[0], x[1]), zip(y, elem)))
len_check = len(elem) == len(y)
return type_check and len_check
elif isinstance(y, dict) and isinstance(next(iter(y.keys())), type):
# {1: 2} in {Int: Int}
elif isinstance(y, dict) and isinstance(elem, dict) and isinstance(next(iter(y.keys())), type):
# TODO:
type_check = True # in_operator(x[next(iter(x.keys()))], next(iter(y.keys())))
type_check = True # contains_operator(next(iter(y.keys())), x[next(iter(x.keys()))])
len_check = len(elem) >= len(y)
return type_check and len_check
else:

View file

@ -16,7 +16,7 @@ from _erg_str import Str, StrMut
from _erg_array import Array
from _erg_dict import Dict
from _erg_set import Set
from _erg_in_operator import in_operator
from _erg_contains_operator import contains_operator
from _erg_mutate_operator import mutate_operator