codon/stdlib/operator.codon

285 lines
3.7 KiB
Python
Raw Normal View History

# Copyright (C) 2022-2024 Exaloop Inc. <https://exaloop.io>
2022-01-24 17:59:45 +08:00
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
def abs(a):
return a.__abs__()
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__abs__ = abs
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
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_
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
def index(a):
return a.__index__()
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__index__ = index
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
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
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
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
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
def neg(a):
return -a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__neg__ = neg
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_
def pos(a):
return +a
2022-01-24 17:59:45 +08:00
2021-09-28 02:02:44 +08:00
__pos__ = pos
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
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
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
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
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
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
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
def countOf(a, b):
n = 0
for x in a:
if x == b:
n += 1
return n
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
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
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
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
Dynamic Polymorphism (#58) * Use Static[] for static inheritance * Support .seq extension * Fix #36 * Polymorphic typechecking; vtables [wip] * v-table dispatch [wip] * vtable routing [wip; bug] * vtable routing [MVP] * Fix texts * Add union type support * Update FAQs * Clarify * Add BSL license * Add makeUnion * Add IR UnionType * Update union representation in LLVM * Update README * Update README.md * Update README * Update README.md * Add benchmarks * Add more benchmarks and README * Add primes benchmark * Update benchmarks * Fix cpp * Clean up list * Update faq.md * Add binary trees benchmark * Add fannkuch benchmark * Fix paths * Add PyPy * Abort on fail * More benchmarks * Add cpp word_count * Update set_partition cpp * Add nbody cpp * Add TAQ cpp; fix word_count timing * Update CODEOWNERS * Update README * Update README.md * Update CODEOWNERS * Fix bench script * Update binary_trees.cpp * Update taq.cpp * Fix primes benchmark * Add mandelbrot benchmark * Fix OpenMP init * Add Module::unsafeGetUnionType * UnionType [wip] [skip ci] * Integrate IR unions and Union * UnionType refactor [skip ci] * Update README.md * Update docs * UnionType [wip] [skip ci] * UnionType and automatic unions * Add Slack * Update faq.md * Refactor types * New error reporting [wip] * New error reporting [wip] * peglib updates [wip] [skip_ci] * Fix parsing issues * Fix parsing issues * Fix error reporting issues * Make sure random module matches Python * Update releases.md * Fix tests * Fix #59 * Fix #57 * Fix #50 * Fix #49 * Fix #26; Fix #51; Fix #47; Fix #49 * Fix collection extension methods * Fix #62 * Handle *args/**kwargs with Callable[]; Fix #43 * Fix #43 * Fix Ptr.__sub__; Fix polymorphism issues * Add typeinfo * clang-format * Upgrade fmtlib to v9; Use CPM for fmtlib; format spec support; __format__ support * Use CPM for semver and toml++ * Remove extension check * Revamp str methods * Update str.zfill * Fix thunk crashes [wip] [skip_ci] * Fix str.__reversed__ * Fix count_with_max * Fix vtable memory allocation issues * Add poly AST tests * Use PDQsort when stability does not matter * Fix dotted imports; Fix issues * Fix kwargs passing to Python * Fix #61 * Fix #37 * Add isinstance support for unions; Union methods return Union type if different * clang-format * Nicely format error tracebacks * Fix build issues; clang-format * Fix OpenMP init * Fix OpenMP init * Update README.md * Fix tests * Update license [skip ci] * Update license [ci skip] * Add copyright header to all source files * Fix super(); Fix error recovery in ClassStmt * Clean up whitespace [ci skip] * Use Python 3.9 on CI * Print info in random test * Fix single unions * Update random_test.codon * Fix polymorhic thunk instantiation * Fix random test * Add operator.attrgetter and operator.methodcaller * Add code documentation * Update documentation * Update README.md * Fix tests * Fix random init Co-authored-by: A. R. Shajii <ars@ars.me>
2022-12-05 08:45:21 +08:00
def attrgetter(attr: Static[str]):
def getter(obj):
return getattr(obj, attr)
return getter
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
@overload
def itemgetter(item: Static[int]):
return lambda o: o[item]
Dynamic Polymorphism (#58) * Use Static[] for static inheritance * Support .seq extension * Fix #36 * Polymorphic typechecking; vtables [wip] * v-table dispatch [wip] * vtable routing [wip; bug] * vtable routing [MVP] * Fix texts * Add union type support * Update FAQs * Clarify * Add BSL license * Add makeUnion * Add IR UnionType * Update union representation in LLVM * Update README * Update README.md * Update README * Update README.md * Add benchmarks * Add more benchmarks and README * Add primes benchmark * Update benchmarks * Fix cpp * Clean up list * Update faq.md * Add binary trees benchmark * Add fannkuch benchmark * Fix paths * Add PyPy * Abort on fail * More benchmarks * Add cpp word_count * Update set_partition cpp * Add nbody cpp * Add TAQ cpp; fix word_count timing * Update CODEOWNERS * Update README * Update README.md * Update CODEOWNERS * Fix bench script * Update binary_trees.cpp * Update taq.cpp * Fix primes benchmark * Add mandelbrot benchmark * Fix OpenMP init * Add Module::unsafeGetUnionType * UnionType [wip] [skip ci] * Integrate IR unions and Union * UnionType refactor [skip ci] * Update README.md * Update docs * UnionType [wip] [skip ci] * UnionType and automatic unions * Add Slack * Update faq.md * Refactor types * New error reporting [wip] * New error reporting [wip] * peglib updates [wip] [skip_ci] * Fix parsing issues * Fix parsing issues * Fix error reporting issues * Make sure random module matches Python * Update releases.md * Fix tests * Fix #59 * Fix #57 * Fix #50 * Fix #49 * Fix #26; Fix #51; Fix #47; Fix #49 * Fix collection extension methods * Fix #62 * Handle *args/**kwargs with Callable[]; Fix #43 * Fix #43 * Fix Ptr.__sub__; Fix polymorphism issues * Add typeinfo * clang-format * Upgrade fmtlib to v9; Use CPM for fmtlib; format spec support; __format__ support * Use CPM for semver and toml++ * Remove extension check * Revamp str methods * Update str.zfill * Fix thunk crashes [wip] [skip_ci] * Fix str.__reversed__ * Fix count_with_max * Fix vtable memory allocation issues * Add poly AST tests * Use PDQsort when stability does not matter * Fix dotted imports; Fix issues * Fix kwargs passing to Python * Fix #61 * Fix #37 * Add isinstance support for unions; Union methods return Union type if different * clang-format * Nicely format error tracebacks * Fix build issues; clang-format * Fix OpenMP init * Fix OpenMP init * Update README.md * Fix tests * Update license [skip ci] * Update license [ci skip] * Add copyright header to all source files * Fix super(); Fix error recovery in ClassStmt * Clean up whitespace [ci skip] * Use Python 3.9 on CI * Print info in random test * Fix single unions * Update random_test.codon * Fix polymorhic thunk instantiation * Fix random test * Add operator.attrgetter and operator.methodcaller * Add code documentation * Update documentation * Update README.md * Fix tests * Fix random init Co-authored-by: A. R. Shajii <ars@ars.me>
2022-12-05 08:45:21 +08:00
def methodcaller(name: Static[str], *args, **kwargs):
def caller(obj):
return getattr(obj, name)(*args, **kwargs)
return caller
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
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
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
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
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
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
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
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
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
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
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
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
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
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