Skip to content

Commit 83c2148

Browse files
authored
Merge pull request #154 from sharno/nubByEq
make nubByEq more efficient, part of #71
2 parents d218f6f + 8e90603 commit 83c2148

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/Data/Array.purs

+6-4
Original file line numberDiff line numberDiff line change
@@ -926,10 +926,12 @@ nubBy comp xs = case head indexedAndSorted of
926926
-- | ```
927927
-- |
928928
nubByEq :: forall a. (a -> a -> Boolean) -> Array a -> Array a
929-
nubByEq eq xs =
930-
case uncons xs of
931-
Just o -> o.head : nubByEq eq (filter (\y -> not (o.head `eq` y)) o.tail)
932-
Nothing -> []
929+
nubByEq eq xs = ST.run do
930+
arr <- STA.empty
931+
ST.foreach xs \x -> do
932+
e <- not <<< Exports.any (_ `eq` x) <$> (STA.unsafeFreeze arr)
933+
when e $ void $ STA.push x arr
934+
STA.unsafeFreeze arr
933935

934936
-- | Calculate the union of two arrays. Note that duplicates in the first array
935937
-- | are preserved while duplicates in the second array are removed.

0 commit comments

Comments
 (0)