Skip to content

Commit

Permalink
Add arg_sort and test.
Browse files Browse the repository at this point in the history
  • Loading branch information
duvenaud committed Sep 24, 2023
1 parent 2c4b5cd commit f456031
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/sort.dx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def sort(xs: n=>a) -> n=>a given (n|Ix, a|Ord) =
AsList(_, r) = reduce mempty mcombine xlists
unsafe_cast_table(to=n, r)

def arg_sort(xs: n=>a) -> n=>n given (n|Ix|Ord, a|Ord) =
indexed_table = for i. (xs[i], i)
sorted = sort indexed_table
map snd sorted

def (+|)(i:n, delta:Nat) -> n given (n|Ix) =
i' = ordinal i + delta
from_ordinal $ select (i' >= size n) (size n -| 1) i'
Expand Down
6 changes: 6 additions & 0 deletions tests/sort-tests.dx
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ import sort

:p is_sorted $ sort ["Charlie", "Alice", "Bob", "Aaron"]
> True

'### Test argsort
xs = ['1', '2', '3', '4', '9', '8', '7', '6']
ixs = arg_sort xs
sort xs == for i. xs[ixs[i]]
> True

0 comments on commit f456031

Please sign in to comment.