Hacker News new | ask | show | jobs
by mlochbaum 2361 days ago
But Dyalog's sorting actually beats its own Unique!

        cmpx '{(1,2≠/⍵)/⍵} {⍵[⍋⍵]} a'
  1.3E¯2
        ({(1,2≠/⍵)/⍵} {⍵[⍋⍵]} a) ≡ ({⍵[⍋⍵]} ∪a)
  1
The function {⍵[⍋⍵]} (index the argument by its own grade) sorts a vector ascending. {(1,2≠/⍵)/⍵} gets unique elements from a sorted numeric vector by taking all those elements which are first or unequal to their predecessor: 2≠/⍵ tests for inequality on all pairs of adjacent elements, and / uses a boolean vector on the left to filter elements on the right.

The second line tests that this unique-sort code gives the same result as sorting the result of Unique.

We use a radix sort for vectors of 4-byte or larger numbers. Unfortunately we can't use sorting to implement Unique in this way because Unique has to preserve the ordering in the original argument. However, it could be used to implement the sort-Unique or Unique-sort combination.