1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00
codon/stdlib/internal/sort.codon
Ibrahim Numanagić cc634d1940
Improved logic for handling overloaded functions (#10)
* Backport seq-lang/seq@develop fixes

* Backport seq-lang/seq@develop fixes

* Select the last matching overload by default (remove scoring logic); Add dispatch stubs for partial overload support

* Select the last matching overload by default [wip]

* Fix various bugs and update tests

* Add support for partial functions with *args/**kwargs; Fix partial method dispatch

* Update .gitignore

* Fix grammar to allow variable names that have reserved word as a prefix

* Add support for super() call

* Add super() tests; Allow static inheritance to inherit @extend methods

* Support for overloaded functions [wip; base logic done]

* Support for overloaded functions

* Update .gitignore

* Fix partial dots

* Rename function overload 'super' to 'superf'

* Add support for super()

* Add tests for super()

* Add tuple_offsetof

* Add tuple support for super()

* Add isinstance support for inherited classes; Fix review issues

Co-authored-by: A. R. Shajii <ars@ars.me>
2022-01-11 20:39:15 -05:00

50 lines
1.4 KiB
Python

from algorithms.pdqsort import pdq_sort_inplace
from algorithms.insertionsort import insertion_sort_inplace
from algorithms.heapsort import heap_sort_inplace
from algorithms.qsort import qsort_inplace
def sorted(
v: Generator[T],
key = Optional[int](),
algorithm: Optional[str] = None,
reverse: bool = False,
T: type
):
"""
Return a sorted list of the elements in v
"""
newlist = [a for a in v]
if not isinstance(key, Optional):
newlist.sort(key, algorithm, reverse)
else:
newlist.sort(algorithm=algorithm, reverse=reverse)
return newlist
def _sort_list(self, key, algorithm: str):
if algorithm == 'pdq':
pdq_sort_inplace(self, key)
elif algorithm == 'insertion':
insertion_sort_inplace(self, key)
elif algorithm == 'heap':
heap_sort_inplace(self, key)
elif algorithm == 'quick':
qsort_inplace(self, key)
else:
raise ValueError("Algorithm '" + algorithm + "' does not exist")
@extend
class List:
def sort(
self,
key = Optional[int](),
algorithm: Optional[str] = None,
reverse: bool = False
):
alg = ~algorithm if algorithm else 'pdq'
if isinstance(key, Optional):
_sort_list(self, lambda x: x, alg)
else:
_sort_list(self, key, alg)
if reverse:
self.reverse()