@@ -117,17 +117,18 @@ import Prelude
117
117
import Control.Alt ((<|>))
118
118
import Control.Alternative (class Alternative )
119
119
import Control.Lazy (class Lazy , defer )
120
+ import Control.Monad.Eff (foreachE )
120
121
import Control.Monad.Rec.Class (class MonadRec , Step (..), tailRecM2 )
121
122
import Control.Monad.ST (pureST )
122
- import Data.Array.ST (unsafeFreeze , emptySTArray , pokeSTArray , pushSTArray , modifySTArray , withArray )
123
+ import Data.Array.ST (unsafeFreeze , unsafeThaw , emptySTArray , pokeSTArray , pushSTArray , modifySTArray , withArray )
123
124
import Data.Array.ST.Iterator (iterator , iterate , pushWhile )
124
125
import Data.Foldable (class Foldable , foldl , foldr , traverse_ )
125
126
import Data.Foldable (foldl , foldr , foldMap , fold , intercalate , elem , notElem , find , findMap , any , all ) as Exports
126
127
import Data.Maybe (Maybe (..), maybe , isJust , fromJust )
127
128
import Data.NonEmpty (NonEmpty , (:|))
128
129
import Data.Traversable (scanl , scanr ) as Exports
129
130
import Data.Traversable (sequence , traverse )
130
- import Data.Tuple (Tuple (..), uncurry )
131
+ import Data.Tuple (Tuple (..), fst , snd , uncurry )
131
132
import Data.Unfoldable (class Unfoldable , unfoldr )
132
133
import Partial.Unsafe (unsafePartial )
133
134
@@ -873,8 +874,19 @@ groupBy op xs =
873
874
-- | nub [1, 2, 1, 3, 3] = [1, 2, 3]
874
875
-- | ```
875
876
-- |
876
- nub :: forall a . Eq a => Array a -> Array a
877
- nub = nubBy eq
877
+ nub :: forall a . Ord a => Array a -> Array a
878
+ nub arr = case head indexedAndSorted of
879
+ Nothing -> []
880
+ Just x -> map fst $ sortWith snd $ pureST do
881
+ -- TODO: use NonEmptyArrays here to avoid partial functions
882
+ result <- unsafeThaw $ singleton x
883
+ foreachE indexedAndSorted \pair@(Tuple x' i) -> do
884
+ lst <- fst <<< unsafePartial (fromJust <<< last) <$> unsafeFreeze result
885
+ when (lst /= x') $ void $ pushSTArray result pair
886
+ unsafeFreeze result
887
+
888
+ where
889
+ indexedAndSorted = sort $ mapWithIndex (flip Tuple ) arr
878
890
879
891
-- | Remove the duplicates from an array, where element equality is determined
880
892
-- | by the specified equivalence relation, creating a new array.
0 commit comments