codon/stdlib/operator.codon

359 lines
3.5 KiB
Python
Raw Normal View History

2022-01-24 17:59:45 +08:00
# (c) 2022 Exaloop Inc. All rights reserved.
lt = lambda a, b: a < b
le = lambda a, b: a <= b
eq = lambda a, b: a == b
ne = lambda a, b: a != b
gt = lambda a, b: a > b
ge = lambda a, b: a >= b
2021-09-28 02:02:44 +08:00
__lt__ = lt
__le__ = le
__eq__ = eq
__ne__ = ne
__gt__ = gt
__ge__ = ge
2022-01-24 17:59:45 +08:00
def not_(a) -> bool:
2021-09-28 02:02:44 +08:00
if hasattr(a, "__bool__"):
return not bool(a)
elif hasattr(a, "__len__"):
return len(a) == 0
else:
compile_error("argument has no __bool__ or __len__ methods")
2022-01-24 17:59:45 +08:00
def truth(a) -> bool:
2021-09-28 02:02:44 +08:00
return bool(a)
2022-01-24 17:59:45 +08:00
def is_(a, b) -> bool:
2021-09-28 02:02:44 +08:00
return a is b
2022-01-24 17:59:45 +08:00
def is_not(a, b) -> bool:
2021-09-28 02:02:44 +08:00
return a is not b
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def abs(a):
return a.__abs__()
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__abs__ = abs
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def add(a, b):
return a + b
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__add__ = add
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def and_(a, b):
return a & b
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__and__ = and_
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def floordiv(a, b):
return a // b
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__floordiv__ = floordiv
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def index(a):
return a.__index__()
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__index__ = index
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def inv(a):
return ~a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
invert = inv
__inv__ = inv
__invert__ = inv
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def lshift(a, b):
return a << b
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__lshift__ = lshift
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def mod(a, b):
return a % b
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__mod__ = mod
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def mul(a, b):
return a * b
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__mul__ = mul
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def matmul(a, b):
return a @ b
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__matmul__ = matmul
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def neg(a):
return -a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__neg__ = neg
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def or_(a, b):
return a | b
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__or__ = or_
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def pos(a):
return +a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__pos__ = pos
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def pow(a, b):
return a ** b
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__pow__ = pow
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def rshift(a, b):
return a >> b
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__rshift__ = rshift
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def sub(a, b):
return a - b
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__sub__ = sub
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def truediv(a, b):
return a / b
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__truediv__ = truediv
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def xor(a, b):
return a ^ b
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__xor__ = xor
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def concat(a, b):
return a + b
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__concat__ = concat
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def contains(a, b):
return b in a # intentionally reversed
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__contains__ = contains
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def countOf(a, b):
n = 0
for x in a:
if x == b:
n += 1
return n
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def delitem(a, b):
del a[b]
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__delitem__ = delitem
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def getitem(a, b):
return a[b]
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__getitem__ = getitem
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def indexOf(a, b):
n = 0
for x in a:
if x == b:
return n
n += 1
2022-01-24 17:59:45 +08:00
raise ValueError(f"sequence.index(x): x not in sequence")
2021-09-28 02:02:44 +08:00
def setitem(a, b, c):
a[b] = c
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__setitem__ = setitem
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def length_hint(a, default=0):
if hasattr(a, "__len__"):
return len(a)
elif hasattr(a, "__length_hint__"):
return a.__length_hint__()
else:
return default
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
# attrgetter not implemented
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def itemgetter(*items):
if staticlen(items) == 1:
item = items[0]
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def g(obj):
return obj[item]
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
return g
else:
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def g(obj):
return tuple(obj[item] for item in items)
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
return g
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
# methodcaller not implemented
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def iadd(a, b):
a += b
return a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__iadd__ = iadd
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def iand(a, b):
a &= b
return a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__iand__ = iand
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def iconcat(a, b):
a += b
return a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__iconcat__ = iconcat
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def ifloordiv(a, b):
a //= b
return a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__ifloordiv__ = ifloordiv
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def ilshift(a, b):
a <<= b
return a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__ilshift__ = ilshift
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def imod(a, b):
a %= b
return a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__imod__ = imod
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def imul(a, b):
a *= b
return a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__imul__ = imul
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def imatmul(a, b):
a @= b
return a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__imatmul__ = imatmul
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def ior(a, b):
a |= b
return a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__ior__ = ior
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def ipow(a, b):
a **= b
return a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__ipow__ = ipow
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def irshift(a, b):
a >>= b
return a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__irshift__ = irshift
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def isub(a, b):
a -= b
return a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__isub__ = isub
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def itruediv(a, b):
a /= b
return a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__itruediv__ = itruediv
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
def ixor(a, b):
a ^= b
return a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__ixor__ = ixor