@@ -117,9 +117,10 @@ 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
@@ -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 sorted of
879
+ Nothing -> []
880
+ Just x -> pureST do
881
+ -- TODO: use NonEmptyArrays here to avoid partial functions
882
+ result <- unsafeThaw $ singleton x
883
+ foreachE sorted \x' -> do
884
+ lst <- unsafePartial (fromJust <<< last) <$> unsafeFreeze result
885
+ when (lst /= x') $ void $ pushSTArray result x'
886
+ unsafeFreeze result
887
+
888
+ where
889
+ sorted = sort 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